How can I build multiple binaries with Cargo?

You can specify multiple binaries using [[bin]], as mentioned in the Cargo Book

[[bin]]
name = "daemon"
path = "src/daemon/bin/main.rs"

[[bin]]
name = "client"
path = "src/client/bin/main.rs"

You can run individual binaries with the cargo run command with the --bin <bin-name> option.

Tip: If you instead put these files in src/bin/daemon.rs and src/bin/client.rs, you’ll get two executables named daemon and client as Cargo compiles all files in src/bin into executables with the same name automatically. You need to specify names and paths like in the snippet above only if you don’t follow this convention.

Leave a Comment