Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended directory structure for a Rust project?

Tags:

rust

Where should one put the sources, examples, documentation, unit tests, integration tests, license, benchmarks etc?

like image 932
jolson Avatar asked Jul 09 '16 00:07

jolson


People also ask

How do you organize a rust project?

Rust projects are organized into crates which form a single distribution unit, like a static object or a DLL. Source within a crate is organized into modules, which are like namespaces but provide an additional privacy boundary. Not everything in a module has to be publicly exported to other modules.

What folder are the release binaries stored in rust?

The . cargo/bin directory of your home directory is the default location of Rust binaries. Not only the official binaries like rustup , rustc , cargo , rustfmt , rustdoc , rls and also the binaries you can install via cargo install command, will be stored in this directory.

What is mod rust?

Rust provides a powerful module system that can be used to hierarchically split code in logical units (modules), and manage visibility (public/private) between them. A module is a collection of items: functions, structs, traits, impl blocks, and even other modules.

What is extern crate?

An extern crate declaration specifies a dependency on an external crate. The external crate is then bound into the declaring scope as the identifier provided in the extern crate declaration.


1 Answers

Cargo, the official package manager for Rust, defines some conventions regarding the layout of a 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.
  • Source code goes in the src directory.
  • The default library file is src/lib.rs.
  • The default executable file is src/main.rs.
  • Other executables can be placed in src/bin/*.rs.
  • Integration tests go in the tests directory (unit tests go in each file they're testing).
  • Example executable files go in the examples directory.
  • Benchmarks go in the benches directory.

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. Run cargo new to set up a new executable project or cargo new --lib to set up a new library project.

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):

#[cfg(test)] mod tests {     #[test]     fn it_works() {     } } 
like image 166
Francis Gagné Avatar answered Oct 09 '22 12:10

Francis Gagné