How do I directly compile a rust file to an executable?

Using Arch-based Linux, WITHOUT using the cargo package manager, how do I compile a file that I directly wrote and saved it as *.rs to a binary executable?

rustc example.rs builds it

1 Like

I suggest using --verbose with cargo once to see what it passes to rustc. You can then adapt that as needed.

6 Likes

Note: rustc has optimizations disabled by default. For cases where performance matters, you'll want to use (at minimum) the -O flag:

rustc -O example.rs

(That's a capital letter “o”, not a zero.)

2 Likes

Can I specify -O3 (e.g. rustc -O3 example.rs)?

Use -C opt-level=3 if you want to set a specific optimization level.

-O is currently equivalent to -C opt-level=2.

2 Likes