Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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
                If you look a bit at the run times, you can see it just checks to see if there are new changes, but it exits imediately because it doesn't have changes. Then it just runs.
– Andrei Damian
                Apr 20, 2019 at 8:17

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
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.