知识卡片
公开类型不等于公开所有内部
pub struct不会自动公开字段和方法,需分别声明;pub enum的变体与载荷、pub trait的方法则随接口可见。模块项默认可由本模块及其子模块访问。外部可达性还取决于路径和重导出,不能只看到某行pub就断定库用户一定能访问。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 4. Dependencies Visibility Syntax · 查看原书依据
Visibility Syntax Rust’s basic unit of visibility is the module. By default, a module’s items (types, methods, constants) are private and accessible only to code in the same module and its submodules. Code that needs to be more widely available is marked with the pub keyword, making it public to some other scope. For most Rust syntactic features, making the
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第4章《Visibility Syntax》(spine8;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch04.html;u120-ch04-007)。
- 结论依据:原文在“Visibility Syntax”附近的定义、例子或代码支撑本卡核心说明:pub struct不会自动公开字段和方法,需分别声明;pub enum的变体与载荷、pub trait的方法则随接口可见。边界按已读前提解释;示例未执行。
- 原始内容:Visibility Syntax Rust’s basic unit of visibility is the module. By default, a module’s items (types, methods, constants) are private and accessible only to code in the same module and its submodules. Code that needs to be more widely available is marked with the pub keyword, making it public to some other scope. For most Rust syntactic features, making the