You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/conditional-compilation.md
+37-2Lines changed: 37 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ r[cfg.general]
31
31
*Conditionally compiled source code* is source code that is compiled only under certain conditions.
32
32
33
33
r[cfg.attributes-macro]
34
-
Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`][attributes] and the built-in [`cfg` macro].
34
+
Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`][attributes] and the built-in [`cfg!`] and [`cfg_select!`][macros].
35
35
36
36
r[cfg.conditional]
37
37
Whether to compile can depend on the target architecture of the compiled crate, arbitrary values passed to the compiler, and other things further described below.
@@ -466,11 +466,45 @@ let machine_kind = if cfg!(unix) {
466
466
println!("I'm running on a {} machine!", machine_kind);
467
467
```
468
468
469
+
r[cfg.cfg_select]
470
+
### The `cfg_select` macro
471
+
472
+
The built-in `cfg_select` macro expands to the right-hand side of the first configuration predicate that evaluates to `true`.
473
+
474
+
For example:
475
+
476
+
```rust
477
+
# #![feature(cfg_select)
478
+
cfg_select! {
479
+
unix => {
480
+
fn foo() { /* unix specific functionality */ }
481
+
}
482
+
target_pointer_width ="32"=> {
483
+
fn foo() { /* non-unix, 32-bit functionality */ }
484
+
}
485
+
_ => {
486
+
fn foo() { /* fallback implementation */ }
487
+
}
488
+
}
489
+
```
490
+
The `cfg_select` macro can also be used in expression position:
491
+
492
+
```rust
493
+
# #![feature(cfg_select)
494
+
let is_unix_str = cfg_select! {
495
+
unix =>"unix",
496
+
_ =>"not unix",
497
+
};
498
+
```
499
+
500
+
A `_` can be used to write a configuration predicate that always evaluates to `true`.
0 commit comments