Rename the default Cargo binary to be different than the package name

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

In ./target/debug/ we can see the binary name: digital-garden-steps. This is the name of our package and we want the actual binary that users install to be named garden instead.

To do this we can change the auto-discovered src/main.rs binary and change the name using a bin config in the Cargo.toml file.

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

Then we see ./target/debug/garden get created when we cargo run or cargo build.

Chris Biscardi: [0:00] After running cargo run, we can see the path as target/debug/digital_garden. That means the name of our binary is digital_garden, which is the same name as the package. If we bring up in Cargo.toml, we can confirm that.

[0:14] We can change the name of the binary by using a bin field. Note that because Cargo packages can have multiple binaries, this is an array. We'll rename the binary to garden and specify the path as being src/main.rs. We could change the path, but it is conventional to have the default binary be src/main, while the default library is src/lib.

[0:39] Now we could see that we run target/debug/garden when we run cargo run. If we look in target/debug, we can see a binary called digital_garden and a binary called garden. This is because digital_garden is left over from the first build, when the binary was still named digital_garden, while garden is the new binary name.