Rust by Example

11.3.1 Custom

Some conditionals like target_os are implicitly provided by rustc, but custom conditionals must be passed to rustc using the --cfg flag.

fn conditional_function() {
    println!("condition met!")

fn main() {

Without the custom cfg flag:

$ rustc && ./custom
No such file or directory (os error 2)

With the custom cfg flag:

$ rustc --cfg some_condition && ./custom
condition met!