Introduction
The standard Rust project structure is defined by Cargo, Rust’s build system and package manager.
- To create a new project run
cargo new <project name> - This creates a conventional project layout that the Rust community follows almost universally.
Standard Rust Project Structure (for a binary crate)
my_project/
├── Cargo.toml # Project metadata, dependencies, etc.
├── Cargo.lock # Locked dependency versions (commit for apps, ignore for libs)
├── src/
│ ├── main.rs # Entry point for binary crates
│ └── lib.rs # (only for library crates or binaries with lib)
├── tests/ # Integration tests (optional but recommended)
│ └── integration.rs
├── examples/ # Example programs (optional)
│ └── example.rs
├── benches/ # Benchmarks (optional)
│ └── benchmark.rs
└── README.md # Project documentation
For a Library Crate (cargo new my_lib --lib)
my_lib/
├── Cargo.toml
├── src/
│ └── lib.rs # Public API surface
└── tests/ # Integration tests
Recommended Best Practices (Community Standard)
1. Use src/bin/ for multiple binaries
src/
├── lib.rs
├── main.rs # Default binary
└── bin/
├── cli.rs # `cargo run --bin cli`
└── server.rs
2. Organize large projects with modules
src/
├── main.rs
├── lib.rs
├── models/
│ ├── user.rs
│ └── post.rs
├── services/
│ └── auth.rs
└── utils/
└── helpers.rs
Use mod declarations in lib.rs or main.rs:
// lib.rs or main.rs
mod models;
mod services;
mod utils;
3. Keep main.rs thin
// main.rs
fn main() {
my_project::run();
}
// lib.rs
pub fn run() {
// actual logic
}
4. Use tests/ for integration tests
// tests/integration.rs
use my_project;
#[test]
fn it_works() {
assert_eq!(my_project::add(2, 2), 4);
}
5. Use examples/ for usage demos
cargo run --example hello
6. Add common config files
├── .gitignore
├── rustfmt.toml # Code formatting
├── clippy.toml # Linter config
└── deny.toml # Cargo-deny for security
Example: Well-Structured App
my_app/
├── Cargo.toml
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── config/
│ │ └── mod.rs
│ ├── handlers/
│ │ └── mod.rs
│ └── models/
│ ├── mod.rs
│ └── user.rs
├── tests/
│ └── api_tests.rs
├── examples/
│ └── quickstart.rs
└── README.md
Official Resources
- The Rust Book – Package Layout
- Cargo Documentation – Workspace & Project Layout
- Rust API Guidelines – Crate Layout
Summary: Follow Cargo Conventions
| File/Dir | Purpose |
|---|---|
Cargo.toml |
Config & dependencies |
src/main.rs |
Binary entry |
src/lib.rs |
Library entry |
src/bin/ |
Multiple binaries |
tests/ |
Integration tests |
examples/ |
Example code |
benches/ |
Benchmarks |
Stick to this structure — it’s what tools, IDEs, and the community expect.
By following this convention, tools like rust-analyzer and Cargo guide you and work best with the standard layout.
Find me on Twitter/ where I tweet about interesting topics on software development.