Two or more executables in the same Rust crate


Normally if you run cargo new name you will create a crate that has a file called src/ which is the main file of the program that will become the executable. You would be able to compile and run that executable by the cargo run command. If you used cargo build to create the executable file, its name would be the same name that you used for the crate.

Sometimes, however you would like to create several executables using the same crate.

You can do that by telling cargo that some of the additional files are also executables. Here is an example:

Started by creating the crate:

cargo new two-executables

I've removed the src/ to make the others "symmetric", so we will do the same coding for each one of them.

Instead of that I've added two simple files. Both of them have a main function.


fn main() {
    println!("Hello, One!");


fn main() {
    println!("Hello, Two");

Then I've added a bin section for each one of the binaries


name = "two-executables"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at


name = "one_exe"
path = "src/"

name = "two"
path = "src/"

This way I can run them a

cargo run --bin one_exe


cargo run --bin two

As you can see the name field does not have to be the same as the name of the file, and the name field is the one that determines how this program will be called.

In target/debug/ you will see the two executables called one_exe and two.

Cargo build to build all of them at once

If you run

cargo build

of maybe

cargo build --release

all the executables will be built at once.


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo