Pass in optimisation flags in Cargo

I'd like to compile using different opt flags (0, 1, 2, 3, "s", "z") but I don't see how I can do this via cargo. I've tried cargo rustc but it turns out the -O flag doesn't exist for that.

I tried using custom profiles via the "named-features" cargo nightly feature, but it's causing problems for my rust-analyser and I don't think I can handle without it (it's just too useful).

How can I use optimisation flags in cargo?

You want the opt-level option. You can apply it to the built in profiles as shown on that page.

I wanted to avoid this since I'm trying to mass compile rust binaries at every single optimisation level. Custom profiles allowed me to cover each opt-level as a unique profile.. at the cost of using nightly.

Looks like if I want to achieve what I want I'd have to include a script that edits cargo.toml and changes the opt-level.

You can set CARGO_PROFILE_RELEASE_OPT_LEVEL env variable to set opt-level for a specific profile: Environment Variables - The Cargo Book

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.