3.2 文件系统层次结构
02-文件系统层次结构 — Comprehensive Rust
2 分钟阅读
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/modules/filesystem.html
3.2 文件系统层次结构
省略模块内容会告诉 Rust 到另一个文件中查找:
| |
这表示 garden 模块的内容位于 src/garden.rs。类似地,garden::vegetables 模块可以位于 src/garden/vegetables.rs。
crate 根位于:
src/lib.rs(库 crate)src/main.rs(二进制 crate)
文件中定义的模块也可以用「内部文档注释」(inner doc comments)来写文档。这类注释记录的是包含它们的项——此处即模块本身。
| |
在 Rust 2018 之前,模块需要放在
module/mod.rs而不是module.rs;2018 及之后的 edition 仍支持这种写法。引入
filename.rs作为filename/mod.rs的替代方案,主要是因为大量名为mod.rs的文件在 IDE 中难以区分。即便主模块是单个文件,更深层的嵌套仍可使用文件夹:
src/ ├── main.rs ├── top_module.rs └── top_module/ └── sub_module.rs可以用编译器指令改变 Rust 查找模块的位置:
1 2 3 4 5// Copyright 2022 Google LLC // SPDX-License-Identifier: Apache-2.0 # #[path = "some/path.rs"] mod some_module;例如,若希望把某个模块的测试放在名为
some_module_test.rs的文件中(类似 Go 的惯例),这就很有用。