知识卡片
模块可见性逐段检查
模块默认保护内部定义,祖先无法直接读取子模块的私有项,子模块可使用祖先项。pub mod 使这个模块可供有权到达它的调用者访问,内部函数、类型等仍分别决定是否公开。可达性要结合调用位置和路径各段,例如根层调用者能看见同层私有模块名,却未必能穿过它内部的私有子模块。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用包、crate 和模块管理增长中的项目 Paths for Referring to an Item in the Module Tree · 查看原书依据
What happened? Adding the pub keyword in front of mod hosting makes the module public. With this change, if we can access front_of_house, we can access hosting. But the contents of hosting are still private; making the module public doesn’t make its contents public. The pub keyword on a module only lets code in its ancestor modules refer to it, not access its inner code.
参考来源
- 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0094)
- 结论依据:只公开 hosting 后诊断转向内部函数,证明公开容器不会级联开放所有成员。
- 原始内容:What happened? Adding the pub keyword in front of mod hosting makes the module public. With this change, if we can access front_of_house, we can access hosting. But the contents of hosting are still private; making the module public doesn’t make its contents public. The pub keyword on a module only lets code in its ancestor modules refer to it, not access its inner code.