Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Package Layout

Cargo uses conventions for file placement to make it easy to dive into a new Cargo package:

.
β”œβ”€β”€ Cargo.lock
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lib.rs
β”‚   β”œβ”€β”€ main.rs
β”‚   └── bin/
β”‚       β”œβ”€β”€ named-executable.rs
β”‚       β”œβ”€β”€ another-executable.rs
β”‚       └── multi-file-executable/
β”‚           β”œβ”€β”€ main.rs
β”‚           └── some_module.rs
β”œβ”€β”€ benches/
β”‚   β”œβ”€β”€ large-input.rs
β”‚   └── multi-file-bench/
β”‚       β”œβ”€β”€ main.rs
β”‚       └── bench_module.rs
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ simple.rs
β”‚   └── multi-file-example/
β”‚       β”œβ”€β”€ main.rs
β”‚       └── ex_module.rs
└── tests/
    β”œβ”€β”€ some-integration-tests.rs
    └── multi-file-test/
        β”œβ”€β”€ main.rs
        └── test_module.rs
  • Cargo.toml and Cargo.lock are stored in the root of your package (package root).
  • 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/.
  • Benchmarks go in the benches directory.
  • Examples go in the examples directory.
  • Integration tests go in the tests directory.

If a binary, example, bench, or integration test consists of multiple source files, place a main.rs file along with the extra modules within a subdirectory of the src/bin, examples, benches, or tests directory. The name of the executable will be the directory name.

Note: By convention, binaries, examples, benches and integration tests follow kebab-case naming style, unless there are compatibility reasons to do otherwise (e.g. compatibility with a pre-existing binary name). Modules within those targets are snake_case following the Rust standard.

You can learn more about Rust’s module system in the book.

See Configuring a target for more details on manually configuring targets. See Target auto-discovery for more information on controlling how Cargo automatically infers target names.