Skip to content

Commit 52b3071

Browse files
committed
Move the cfg example to the intro
1 parent 0de49df commit 52b3071

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/conditional-compilation.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,42 @@ r[cfg.attr]
321321
r[cfg.attr.intro]
322322
The `cfg` [attribute] conditionally includes the thing it is attached to based on a configuration predicate.
323323

324+
> [!EXAMPLE]
325+
> Some examples of using `cfg` on functions:
326+
>
327+
> ```rust
328+
> // The function is only included in the build when compiling for macOS
329+
> #[cfg(target_os = "macos")]
330+
> fn macos_only() {
331+
> // ...
332+
> }
333+
>
334+
> // This function is only included when either foo or bar is defined
335+
> #[cfg(any(foo, bar))]
336+
> fn needs_foo_or_bar() {
337+
> // ...
338+
> }
339+
>
340+
> // This function is only included when compiling for a unixish OS with a 32-bit
341+
> // architecture
342+
> #[cfg(all(unix, target_pointer_width = "32"))]
343+
> fn on_32bit_unix() {
344+
> // ...
345+
> }
346+
>
347+
> // This function is only included when foo is not defined
348+
> #[cfg(not(foo))]
349+
> fn needs_not_foo() {
350+
> // ...
351+
> }
352+
>
353+
> // This function is only included when the panic strategy is set to unwind
354+
> #[cfg(panic = "unwind")]
355+
> fn when_unwinding() {
356+
> // ...
357+
> }
358+
> ```
359+
324360
r[cfg.attr.syntax]
325361
```grammar,configuration
326362
@root CfgAttribute -> `cfg` `(` ConfigurationPredicate `)`
@@ -337,41 +373,6 @@ If the predicate is true, the thing is rewritten to not have the `cfg` attribute
337373
r[cfg.attr.crate-level-attrs]
338374
When a crate-level `cfg` has a false predicate, the behavior is slightly different: any crate attributes preceding the `cfg` are kept, and any crate attributes following the `cfg` are removed. This allows `#![no_std]` and `#![no_core]` crates to avoid linking `std`/`core` even if a `#![cfg(...)]` has removed the entire crate.
339375

340-
Some examples on functions:
341-
342-
```rust
343-
// The function is only included in the build when compiling for macOS
344-
#[cfg(target_os = "macos")]
345-
fn macos_only() {
346-
// ...
347-
}
348-
349-
// This function is only included when either foo or bar is defined
350-
#[cfg(any(foo, bar))]
351-
fn needs_foo_or_bar() {
352-
// ...
353-
}
354-
355-
// This function is only included when compiling for a unixish OS with a 32-bit
356-
// architecture
357-
#[cfg(all(unix, target_pointer_width = "32"))]
358-
fn on_32bit_unix() {
359-
// ...
360-
}
361-
362-
// This function is only included when foo is not defined
363-
#[cfg(not(foo))]
364-
fn needs_not_foo() {
365-
// ...
366-
}
367-
368-
// This function is only included when the panic strategy is set to unwind
369-
#[cfg(panic = "unwind")]
370-
fn when_unwinding() {
371-
// ...
372-
}
373-
374-
```
375376

376377
r[cfg.attr.restriction]
377378
The `cfg` attribute is allowed anywhere attributes are allowed.

0 commit comments

Comments
 (0)