Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rust compile my project again with `cargo build --release` followed by `cargo run`?

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
like image 665
Muhammad Naufil Avatar asked Apr 20 '19 06:04

Muhammad Naufil


People also ask

Does cargo run also build?

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.

What does cargo build -- Release do?

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.

Where does cargo build output go?

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 Rust and cargo?

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.


1 Answers

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:

  • The debug build, stored in target/debug/
  • The release build, stored in target/release/

All of these sub-commands allow you to specify which of these profiles to use (not necessarily an exhaustive list):

  • Default: debug (switch to release mode with --release)
    • cargo build
    • cargo run
    • cargo test
    • cargo check
  • Default: release (switch to debug mode with --debug)
    • cargo bench
    • cargo install
like image 167
Lukas Kalbertodt Avatar answered Oct 16 '22 08:10

Lukas Kalbertodt