Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unrecognized option 'release'" when compiling with rustc in release mode

Tags:

rust

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'.
like image 764
Lukas Kalbertodt Avatar asked Aug 09 '17 14:08

Lukas Kalbertodt


1 Answers

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

A bit more detail:

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.

like image 172
Lukas Kalbertodt Avatar answered Oct 02 '22 00:10

Lukas Kalbertodt