#flags #no-alloc #copt-level

no-std opt-level

Get value of rustc -Copt-level= flag

1 stable release

1.0.0 Dec 21, 2025

#91 in No standard library

Download history 1295/week @ 2025-12-21 1551/week @ 2025-12-28 775/week @ 2026-01-04 1873/week @ 2026-01-11 1028/week @ 2026-01-18 1008/week @ 2026-01-25

5,033 downloads per month
Used in 2 crates

MIT/Apache

9KB

opt_level::OPT_LEVEL

github crates.io docs.rs build status

Get the value of rustc's -Copt-level= flag at runtime.

Useful for sizing tests to run fewer iterations in slow build modes.

According to https://doc.rust-lang.org/cargo/reference/profiles.html#opt-level the possible values are:

  • 0: no optimizations
  • 1: basic optimizations
  • 2: some optimizations
  • 3: all optimizations
  • s: optimize for binary size
  • z: optimize for binary size, but also turn off loop vectorization

Example

use rand::rngs::SmallRng;
use rand::{RngCore as _, SeedableRng as _};

const N: usize = if cfg!(miri) {
    500
} else if let b"0" = opt_level::OPT_LEVEL.as_bytes() {
    10_000
} else {
    100_000_000
};

#[test]
fn random_test() {
    let mut rng = SmallRng::from_os_rng();

    for _ in 0..N {
        let bits = rng.next_u64();
        ...
        assert_eq!(..., ...);
    }
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps