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
andCargo.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/
.
- Other executables can be placed in
- 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 aresnake_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.