简体   繁体   English

为什么Rust会先用`cargo build --release`再加上`cargo run`再次编译我的项目?

[英]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 . Rust文档告诉我们, cargo build会在编译后创建一个二进制文件,我们可以通过cargo run执行该文件。 cargo run will again compile the code if it notices any change after cargo build command is executed. 如果在执行cargo build命令后发现任何更改, cargo run将再次编译该代码。 It also says that cargo build --release command creates the final program, which will run faster. 它还说, cargo build --release命令创建最终程序,它将运行得更快。

My question is, why is that when I do cargo build --release , it compiles the code, which is fine. 我的问题是,为什么在我进行cargo build --release时会编译代码,这很好。 But when I execute cargo run , it again compiles the code, even though I haven't changed any code since. 但是,当我执行cargo run时,即使此后未更改任何代码,它仍会再次编译代码。 It is working normally with cargo build , followed by cargo run ie compiling one time with the former command. 它可以正常进行cargo build ,然后进行cargo run即与前一个命令一起编译一次。

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

cargo run attempts to run the debug build of your project. cargo run尝试运行项目的调试版本。 Use cargo run --release instead. 请改用cargo run --release A cargo build --release followed by cargo run --release won't compile again. cargo build --release然后是cargo run --release将不会再次编译。

Cargo maintains two pretty much completely independent sets of build artifacts: Cargo维护两组几乎完全独立的构建工件:

  • The debug build, stored in target/debug/ 调试版本,存储在target/debug/
  • The release build, stored in target/release/ 发布版本,存储在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 ) 默认值:debug (使用--release切换到发布模式)
    • cargo build
    • cargo run
    • cargo test
    • cargo check
  • Default: release (switch to debug mode with --debug ) 默认值:release (使用--debug切换到调试模式)
    • cargo bench
    • cargo install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM