[英]What is the recommended directory structure for a Rust project?
应该在哪里放置源,示例,文档,单元测试,集成测试,许可证,基准测试等 ?
Cargo, the official package manager for Rust, defines some conventions regarding the layout of a Rust crate : Cargo是Rust的官方软件包管理器, 定义了一些关于Rust crate布局的约定 :
. ├── Cargo.lock ├── Cargo.toml ├── benches │ └── large-input.rs ├── examples │ └── simple.rs ├── src │ ├── bin │ │ └── another_executable.rs │ ├── lib.rs │ └── main.rs └── tests └── some-integration-tests.rs
Cargo.toml
andCargo.lock
are stored in the root of your project.Cargo.toml
和Cargo.lock
存储在项目的根目录中。- Source code goes in the
src
directory.源代码进入
src
目录。- The default library file is
src/lib.rs
.默认库文件是
src/lib.rs
- The default executable file is
src/main.rs
.默认的可执行文件是
src/main.rs
- Other executables can be placed in
src/bin/*.rs
.其他可执行文件可以放在
src/bin/*.rs
。- Integration tests go in the
tests
directory (unit tests go in each file they're testing).集成测试进入
tests
目录(单元测试进入他们正在测试的每个文件中)。- Example executable files go in the
examples
directory.示例可执行文件放在
examples
目录中。- Benchmarks go in the
benches
directory.基准进入
benches
目录。These are explained in more detail in the manifest description .
这些在清单描述中有更详细的解释。
By following this standard layout, you'll be able to use Cargo's commands to build, run and test your project easily. 通过遵循此标准布局,您将能够使用Cargo的命令轻松构建,运行和测试项目。 Run
cargo new
to set up a new executable project or cargo new --lib
to set up a new library project. 运行
cargo new
以设置新的可执行项目或cargo new --lib
以设置新的库项目。
Additionally, documentation for libraries is often written in documentation comments (comments that start with ///
before any item, or //!
to document the parent item). 此外,库的文档通常写在文档注释中(注释在任何项之前以
///
开头,或者//!
以记录父项)。 Also, the license is usually put at the root. 此外,许可证通常放在根目录下。
Unit tests, as mentioned above, are written in the same module as the functions they're testing. 如上所述,单元测试与它们正在测试的功能编写在同一模块中。 Usually, they're put in an inner module.
通常,它们被放入内部模块中。 It looks like this (this is what Cargo generates for a new library with
cargo new --lib
): 它看起来像这样(这是Cargo为一个带有
cargo new --lib
的新库生成的):
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.