知识卡片
拆文件保持模块树
mod a; 在声明位置建立模块 a,并从约定位置读取定义;下一级 mod b; 的文件路径还要反映父模块关系。m.rs 与 m/mod.rs 都可描述模块 m,但同一模块只能选一种。将内联模块搬到相应文件不会改变既有逻辑路径,其他文件用路径访问,无需重复声明。
相关卡片 [[模块树组织定义]]、[[路径起点随移动选择]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用包、crate 和模块管理增长中的项目 Separating Modules into Different Files · 查看原书依据
Note that you only need to load a file using a mod declaration once in your module tree. Once the compiler knows the file is part of the project (and knows where in the module tree the code resides because of where you’ve put the mod statement), other files in your project should refer to the loaded file’s code using a path to where it was declared, as covered in
参考来源
- 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0218)
- 结论依据:原文强调模块只应声明一次,由声明位置确定在树中的位置;换文件不足以改变树。
- 原始内容:Note that you only need to load a file using a mod declaration once in your module tree. Once the compiler knows the file is part of the project (and knows where in the module tree the code resides because of where you’ve put the mod statement), other files in your project should refer to the loaded file’s code using a path to where it was declared, as covered in