Rust documentation teaches us that cargo build
creates a binary file after compiling, which we can execute with cargo run
. cargo run
will again compile the code if it notices any change after cargo build
command is executed. It also says that cargo build --release
command creates the final program, which will run faster.
My question is, why is that when I do cargo build --release
, it compiles the code, which is fine. But when I execute cargo run
, it again compiles the code, even though I haven't changed any code since. It is working normally with cargo build
, followed by cargo run
i.e compiling one time with the former command.
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo build
Compiling variables v0.1.0 (/home/naufil/Desktop/rust/20April/variables)
Finished dev [unoptimized + debuginfo] target(s) in 0.35s
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/variables`
Hello, world! 6
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo build --release
Compiling variables v0.1.0 (/home/naufil/Desktop/rust/20April/variables)
Finished release [optimized] target(s) in 0.34s
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo run
Compiling variables v0.1.0 (/home/naufil/Desktop/rust/20April/variables)
Finished dev [unoptimized + debuginfo] target(s) in 0.23s
Running `target/debug/variables`
Hello, world! 6
We can build a project using cargo build . We can build and run a project in one step using cargo run . We can build a project without producing a binary to check for errors using cargo check . Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
cargo build --release puts the resulting binary in target/release instead of target/debug . Compiling in debug mode is the default for development-- compilation time is shorter since the compiler doesn't do optimizations, but the code will run slower. Release mode takes longer to compile, but the code will run faster.
Cargo stores the output of a build into the "target" directory. By default, this is the directory named target in the root of your workspace. To change the location, you can set the CARGO_TARGET_DIR environment variable, the build. target-dir config value, or the --target-dir command-line flag.
What is Cargo in Rust? Cargo is Rust's build system and package manager. With this tool, you'll get a repeatable build because it allows Rust packages to declare their dependencies in the manifest, Cargo. toml . When you install Rust through rustup , Cargo is also installed.
cargo run
attempts to run the debug build of your project. Use cargo run --release
instead. A cargo build --release
followed by cargo run --release
won't compile again.
Cargo maintains two pretty much completely independent sets of build artifacts:
target/debug/
target/release/
All of these sub-commands allow you to specify which of these profiles to use (not necessarily an exhaustive list):
--release
)
cargo build
cargo run
cargo test
cargo check
--debug
)
cargo bench
cargo install
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With