Login
You're viewing the techdon.dev public feed.
  • Jun 4, 2026, 9:35 AM

    Rust constants are slowly but surely getting more powerful!

    On stable you can parse constants from environment variables. I remember having to pull in weird crates & macros to be able to do this.

    #rustlang

    const TASK_COUNT: usize = {
    let input = if let Some(string) = option_env!("PERFMON_TASK_COUNT") {
        string
    } else {
        "16"
    };
    match usize::from_str_radix(input, 10) {
        Ok(val @ 2..) => val,
        Ok(_) => panic!("PERFMON_TASK_COUNT must be 2 or higher"),
        Err(_e) => panic!("Could not parse `PERFMON_TASK_COUNT`"),
    }
};
    💬 0🔄 6⭐ 9