知识卡片

普通读书笔记卡 · 10645.d · 生成模型:未记录

公开类型有不同粒度

理解等级:中级

pub struct 暴露类型名,字段仍需要分别公开。只要存在调用方无法填写的私有字段,库就可用公开构造函数提供合规实例。公开 enum 的变体则随类型公开。库可借此决定用户能自由构造哪些状态、修改哪些字段。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0127) - 结论依据:原文直接对比公开结构体和公开枚举的默认成员可见性。 - 原始内容:Enums aren’t very useful unless their variants are public; it would be annoying to have to annotate all enum variants with pub in every case, so the default for enum variants is to be public. Structs are often useful without their fields being public, so struct fields follow the general rule of everything being private by default unless annotated with pub.