cfg

Conditional compilation is possible through two different operators:

  • the cfg attribute: #[cfg(...)] in attribute position
  • the cfg! macro: cfg!(...) in boolean expressions

Both utilize identical argument syntax.

1. // This function only gets compiled if the target OS is linux
2. #[cfg(target_os = "linux")]
3. fn are_you_on_linux() {
4. println!("You are running linux!");
5. }

7. // And this function only gets compiled if the target OS is *not* linux
8. #[cfg(not(target_os = "linux"))]
9. fn are_you_on_linux() {
10. println!("You are *not* running linux!");
11. }

13. fn main() {
14. are_you_on_linux();

16. println!("Are you sure?");
17. if cfg!(target_os = "linux") {
18. println!("Yes. It's definitely linux!");
19. } else {
20. println!("Yes. It's definitely *not* linux!");
21. }
22. }

See also:

the reference, cfg!, and macros.