I'd like to compile a project multiple times with different settings (for different environments).
Is there a simple way to pass an argument to the compiler that will be used as a numeric constant?
$ SAMPLE_RATE=44100 cargo build
So far I've tried:
pub const SAMPLE_RATE: u32 = env!("SAMPLE_RATE").parse::<u32>().unwrap();
(this naturally fails because
parse cannot be called in a constant manner)
I thought about using a
build.rs and write out a single-lined source file based on the environment variables during the build:
pub const SAMPLE_RATE: u32 = $$to be replaced by build-script$$;
This would work, but cargo's documentation states:
Build scripts may save any output files in the directory specified in the
OUT_DIRenvironment variable. Scripts should not modify any files outside of that directory."
… and it's a bit cumbersome