Multiple binaries in a single Rust project
In Rust, managing multiple binaries within a single project can be an essential skill, especially for developers working on complex applications, tools, or utility suites. Whether you’re building a suite of command-line tools, a server with multiple entry points, or just experimenting with different approaches to a problem, understanding how to organize and manage multiple binaries can streamline your development process and improve code maintainability.
Rust’s powerful package manager, Cargo, provides flexible options for defining and running multiple binaries within a single project. This flexibility allows you to keep related binaries together, share common code, and build and run each binary independently. There are several ways to achieve this, from utilizing the src/bin
directory to specifying binaries directly in the Cargo.toml
file, or even structuring your project as a workspace for larger, more complex setups.
cargo new my_project
cd my_project
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "binary1"
path = "src/binary1.rs"
[[bin]]
name = "binary2"
path = "src/binary2.rs"
touch src/binary1.rs
touch src/binary2.rs
fn main() {
println!("This is binary 1");
}
fn main() {
println!("This is binary 2");
}
cargo run --bin binary1
cargo run --bin binary2