简体   繁体   English

Rust项目的推荐目录结构是什么?

[英]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 and Cargo.lock are stored in the root of your project. Cargo.tomlCargo.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM