自定义条件

有部分条件如 target_os 在使用 rustc 时会隐式地提供,但是自定义条件必须使用 --cfg 标记来传给 rustc

  1. #[cfg(some_condition)]
  2. fn conditional_function() {
  3. println!("condition met!")
  4. }
  5. fn main() {
  6. conditional_function();
  7. }

不使用自定义的 cfg 标记:

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

使用自定义的 cfg 标记:

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