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
<!-- should we say they're active attributes here? -->
327
-
328
-
r[cfg.attr.general]
329
-
The `cfg`[attribute] conditionally includes the thing it is attached to based
330
-
on a configuration predicate.
365
+
r[cfg.attr.allowed-positions]
366
+
The `cfg` attribute is allowed anywhere attributes are allowed.
331
367
332
-
r[cfg.attr.syntax-explanation]
333
-
It is written as `cfg`, `(`, a configuration predicate, and finally `)`.
368
+
r[cfg.attr.duplicates]
369
+
Multiple `cfg` attributes may be specified. The form to which the attribute is attached will not be included if any of the `cfg` predicates are false except as described in [cfg.attr.crate-level-attrs].
334
370
335
371
r[cfg.attr.effect]
336
-
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
337
-
on it. If the predicate is false, the thing is removed from the source code.
372
+
If the predicate is true, the form is rewritten to not have the `cfg` attribute on it. If the predicate is false, the form is removed from the source code.
338
373
339
374
r[cfg.attr.crate-level-attrs]
340
-
When a crate-level `cfg` has a false predicate, the behavior is slightly
341
-
different: any crate attributes preceding the `cfg` are kept, and any crate
342
-
attributes following the `cfg` are removed. This allows `#![no_std]` and
343
-
`#![no_core]` crates to avoid linking `std`/`core` even if a `#![cfg(...)]` has
344
-
removed the entire crate.
345
-
346
-
Some examples on functions:
347
-
348
-
```rust
349
-
// The function is only included in the build when compiling for macOS
350
-
#[cfg(target_os ="macos")]
351
-
fnmacos_only() {
352
-
// ...
353
-
}
354
-
355
-
// This function is only included when either foo or bar is defined
356
-
#[cfg(any(foo, bar))]
357
-
fnneeds_foo_or_bar() {
358
-
// ...
359
-
}
360
-
361
-
// This function is only included when compiling for a unixish OS with a 32-bit
362
-
// architecture
363
-
#[cfg(all(unix, target_pointer_width ="32"))]
364
-
fnon_32bit_unix() {
365
-
// ...
366
-
}
367
-
368
-
// This function is only included when foo is not defined
369
-
#[cfg(not(foo))]
370
-
fnneeds_not_foo() {
371
-
// ...
372
-
}
373
-
374
-
// This function is only included when the panic strategy is set to unwind
375
-
#[cfg(panic ="unwind")]
376
-
fnwhen_unwinding() {
377
-
// ...
378
-
}
379
-
380
-
```
381
-
382
-
r[cfg.attr.restriction]
383
-
The `cfg` attribute is allowed anywhere attributes are allowed.
375
+
When a crate-level `cfg` has a false predicate, the crate itself still exists. Any crate attributes preceding the `cfg` are kept, and any crate attributes following the `cfg` are removed as well as removing all of the following crate contents.
376
+
377
+
> [!EXAMPLE]
378
+
> The behavior of not removing the preceding attributes allows you to do things such as include `#![no_std]` to avoid linking `std` even if a `#![cfg(...)]` has otherwise removed the contents of the crate. For example:
379
+
>
380
+
> <!-- ignore: test infrastructure can't handle no_std -->
381
+
> ```rust,ignore
382
+
> // This `no_std` attribute is kept even though the crate-level `cfg`
0 commit comments