Skip to content

Commit 4c29a19

Browse files
authored
Rollup merge of rust-lang#39374 - durka:patch-34, r=steveklabnik
reference: clarify #[cfg] section Closes rust-lang#39370, but maybe we (or clippy) should add a warning too.
2 parents 1d67bb9 + 620074d commit 4c29a19

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/doc/reference.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -2114,10 +2114,15 @@ Sometimes one wants to have different compiler outputs from the same code,
21142114
depending on build target, such as targeted operating system, or to enable
21152115
release builds.
21162116

2117-
There are two kinds of configuration options, one that is either defined or not
2118-
(`#[cfg(foo)]`), and the other that contains a string that can be checked
2119-
against (`#[cfg(bar = "baz")]`). Currently, only compiler-defined configuration
2120-
options can have the latter form.
2117+
Configuration options are boolean (on or off) and are named either with a
2118+
single identifier (e.g. `foo`) or an identifier and a string (e.g. `foo = "bar"`;
2119+
the quotes are required and spaces around the `=` are unimportant). Note that
2120+
similarly-named options, such as `foo`, `foo="bar"` and `foo="baz"` may each be set
2121+
or unset independently.
2122+
2123+
Configuration options are either provided by the compiler or passed in on the
2124+
command line using `--cfg` (e.g. `rustc main.rs --cfg foo --cfg 'bar="baz"'`).
2125+
Rust code then checks for their presence using the `#[cfg(...)]` attribute:
21212126

21222127
```
21232128
// The function is only included in the build when compiling for OSX
@@ -2196,7 +2201,10 @@ You can also set another attribute based on a `cfg` variable with `cfg_attr`:
21962201
#[cfg_attr(a, b)]
21972202
```
21982203

2199-
Will be the same as `#[b]` if `a` is set by `cfg`, and nothing otherwise.
2204+
This is the same as `#[b]` if `a` is set by `cfg`, and nothing otherwise.
2205+
2206+
Lastly, configuration options can be used in expressions by invoking the `cfg!`
2207+
macro: `cfg!(a)` evaluates to `true` if `a` is set, and `false` otherwise.
22002208

22012209
### Lint check attributes
22022210

0 commit comments

Comments
 (0)