Skip to content

Commit b787295

Browse files
committed
TRPL: attributes & conditional compilation
1 parent 3860240 commit b787295

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [The Stack and the Heap](the-stack-and-the-heap.md)
1010
* [Debug and Display](debug-and-display.md)
1111
* [Testing](testing.md)
12+
* [Conditional Compilation](conditional-compilation.md)
1213
* [Documentation](documentation.md)
1314
* [Iterators](iterators.md)
1415
* [Concurrency](concurrency.md)
@@ -47,7 +48,6 @@
4748
* [`const`](const.md)
4849
* [Tuple Structs](tuple-structs.md)
4950
* [Attributes](attributes.md)
50-
* [Conditional Compilation](conditional-compilation.md)
5151
* [`type` aliases](type-aliases.md)
5252
* [Casting between types](casting-between-types.md)
5353
* [Associated Types](associated-types.md)

src/doc/trpl/attributes.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
11
% Attributes
22

3-
Coming Soon!
3+
Declarations can be annotated with ‘attributes’ in Rust. They look like this:
4+
5+
```rust
6+
#[test]
7+
# fn foo() {}
8+
```
9+
10+
or like this:
11+
12+
```rust
13+
# mod foo {
14+
#![test]
15+
# }
16+
```
17+
18+
The difference between the two is the `!`, which changes what the attribute
19+
applies to:
20+
21+
```rust,ignore
22+
#[foo]
23+
struct Foo;
24+
25+
mod bar {
26+
#![bar]
27+
}
28+
```
29+
30+
The `#[foo]` attribute applies to the next item, which is the `struct`
31+
declaration. The `#![bar]` attribute applies to the item enclosing it, which is
32+
the `mod` declaration. Otherwise, they’re the same. Both change the meaning of
33+
the item they’re attached to somehow.
34+
35+
For example, consider a function like this:
36+
37+
```rust
38+
#[test]
39+
fn check() {
40+
assert_eq!(2, 1 + 1);
41+
}
42+
```
43+
44+
It is marked with `#[test]`. This means it’s special: when you run
45+
[tests][tests], this function will execute. When you compile as usual, it won’t
46+
even be included. This function is now a test function.
47+
48+
[tests]: testing.html
49+
50+
Attributes may also have additional data:
51+
52+
```rust
53+
#[inline(always)]
54+
fn super_fast_fn() {
55+
# }
56+
```
57+
58+
Or even keys and values:
59+
60+
```rust
61+
#[cfg(target_os = "macos")]
62+
mod macos_only {
63+
# }
64+
```
65+
66+
Rust attributes are used for a number of different things. There is a full list
67+
of attributes [in the reference][reference]. Currently, you are not allowed to
68+
create your own attributes, the Rust compiler defines them.
69+
70+
[reference]: reference.html#attributes
Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
% Conditional Compilation
22

3-
Coming Soon!
3+
Rust has a special attribute, `#[cfg]`, which allows you to compile code
4+
based on a flag passed to the compiler. It has two forms:
5+
6+
```rust
7+
#[cfg(foo)]
8+
# fn foo() {}
9+
10+
#[cfg(bar = "baz")]
11+
# fn bar() {}
12+
```
13+
14+
They also have some helpers:
15+
16+
```rust
17+
#[cfg(any(unix, windows))]
18+
# fn foo() {}
19+
20+
#[cfg(all(unix, target_pointer_width = "32"))]
21+
# fn bar() {}
22+
23+
#[cfg(not(foo))]
24+
# fn not_foo() {}
25+
```
26+
27+
These can nest arbitrarily:
28+
29+
```rust
30+
#[cfg(any(not(unix), all(macos, target_arch = "powerpc")))]
31+
# fn foo() {}
32+
```
33+
34+
As for how to enable or disable these switches, if you’re using Cargo,
35+
they get set in the [`[features]` section][features] of your `Cargo.toml`:
36+
37+
[features]: http://doc.crates.io/manifest.html#the-[features]-section
38+
39+
```toml
40+
[features]
41+
# no features by default
42+
default = []
43+
44+
# The “secure-password” feature depends on the bcrypt package.
45+
secure-password = ["bcrypt"]
46+
```
47+
48+
When you do this, Cargo passes along a flag to `rustc`:
49+
50+
```
51+
--cfg feature="${feature_name}"
52+
```
53+
54+
The sum of these `cfg` flags will determine which ones get activated, and
55+
therefore, which code gets compiled.
56+
57+
## cfg_attr
58+
59+
You can also set another attribute based on a `cfg` variable with `cfg_attr`:
60+
61+
```rust
62+
#[cfg_attr(a, b)]
63+
# fn foo() {}
64+
```
65+
66+
Will be the same as `#[b]` if `a` is set by `cfg` attribute, and nothing otherwise.

0 commit comments

Comments
 (0)