Huy's Blog

Multiple binaries in a Rust project

If you have a project that needs to compile into multiple binaries or libraries, you can config it in Cargo.toml like this:

...

[[lib]]
name = "<your-lib-name>"
src = "<path-to-source>"

[[bin]]
name = "<your-binary-name>"
src = "<path-to-source>"

...

For example, I am working on a 6502 emulator, and I need to build some tool along with my emulator, so this is my Cargo.toml:

...

[[bin]]
name = "romgen"
src = "src/romgen.rs"

[[bin]]
name = "emulator"
src = "src/main.rs"

Please note that after config the Cargo.toml to build multiple binaries, you need to specify the binary name everytime you run the project with cargo run:

cargo run --bin <binary-name>

But it's annoying, it would be great if we could have a default binary that will execute every time we use cargo run. There is also an issue created for this.