Некоторые условия, например, target_os
предоставляются компилятором. Если мы хотим создать собственные условия, то их необходимо передать компилятору используя флаг --cfg
.
// custom.rs
#[cfg(some_condition)]
fn conditional_function() {
println!("condition met!")
}
fn main() {
conditional_function();
}
Без указания флага cfg
:
$ rustc custom.rs && ./custom
No such file or directory (os error 2)
С указанием флага cfg
:
$ rustc --cfg some_condition custom.rs && ./custom
condition met!