I have a simple .rs file and want to compile it using rustc. I always heard about how important it is to compile in release mode, because otherwise my Rust program will be slow.
However, if I use the often quoted --release flag, it doesn't work. What is everyone talking about if the flag doesn't even exist?
$ rustc --release foo.rs
error: Unrecognized option: 'release'.
You made a simple mistake: The --release flag is a flag for cargo. The easiest way you can turn on optimizations with rustc is using the -O flag.
Examples:
rustc -O foo.rs
rustc -C opt-level=3 foo.rs
cargo build --release
You can compile your Rust program with various levels of optimization. rustc -C help says:
-C opt-level=val -- optimize with possible levels 0-3, s, or z
To have the most control, you should compile with rustc -C opt-level=3 foo.rs (or any other level). However, this isn't always necessary. Often, you can use -O; rustc --help says:
-O Equivalent to -C opt-level=2
Most of the time rustc -O foo.rs is the right choice.
Cargo, on the other hand, works a bit different; or at least the --release flag does. Cargo has different profiles which dictate how cargo invokes rustc. The most important ones are dev (development- or debug-mode) and release. The --release flag switches the default profile from dev to release (duh!). This potentially changes many flags of the rustc invocation. Most importantly, it changes the values for opt-level and debuginfo:
$ rustc -C debuginfo=2 -C opt-level=0 # in `dev` profile/debug mode
$ rustc -C debuginfo=0 -C opt-level=3 # in `release` profile
You can even change the settings for each profile in your Cargo.toml. You can find more information on profiles and on the default values used in this Cargo documentation.
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