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
*Conditionally compiled source code* is source code that may or may not be
26
-
considered a part of the source code depending on certain conditions. <!-- This
27
-
definition is sort of vacuous --> Source code can be conditionally compiled
28
-
using the [attributes][`cfg`] and [`cfg_attr`] and the built-in [`cfg` macro].
29
-
These conditions are based on the target architecture of the compiled crate,
30
-
arbitrary values passed to the compiler, and a few other miscellaneous things
31
-
further described below in detail.
25
+
*Conditionally compiled source code* is source code that is compiled only under certain conditions. Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`][attributes] and the built-in [`cfg` macro]. 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.
32
26
33
27
Each form of conditional compilation takes a _configuration predicate_ that
34
28
evaluates to true or false. The predicate is one of the following:
35
29
36
-
* A configuration option. It is true if the option is set and false if it is
30
+
* A configuration option. The predicate is true if the option is set, and false if it is
37
31
unset.
38
-
*`all()` with a comma separated list of configuration predicates. It is false
39
-
if at least one predicate is false. If there are no predicates, it is true.
40
-
*`any()` with a comma separated list of configuration predicates. It is true
41
-
if at least one predicate is true. If there are no predicates, it is false.
42
-
*`not()` with a configuration predicate. It is true if its predicate is false
43
-
and false if its predicate is true.
32
+
*`all()` with a comma-separated list of configuration predicates. It is true precisely if all of the given predicates are true.
33
+
*`any()` with a comma-separated list of configuration predicates. It is true precisely if at least one of the given predicates is true.
34
+
*`not()` with a configuration predicate. It is true precisely if the given predicate is false.
44
35
45
-
_Configuration options_ are names and key-value pairs that are either set or
46
-
unset. Names are written as a single identifier such as, for example, `unix`.
47
-
Key-value pairs are written as an identifier, `=`, and then a string. For
48
-
example, `target_arch = "x86_64"` is a configuration option.
36
+
_Configuration options_ are either names or key-value pairs, and are either set or
37
+
unset. Configuration options that are names, such as `unix`, are specified by simply writing them.
38
+
Configuration options that are key-value pairs are written in the form `key = "value"`, such as `target_arch = "x86_64"`.
49
39
50
-
> **Note**: Whitespace around the `=` is ignored. `foo="bar"` and `foo = "bar"`
51
-
> are equivalent configuration options.
40
+
> **Note**: Whitespace around the `=` is ignored, so `foo="bar"` and `foo = "bar"` are equivalent.
52
41
53
-
Keys are not unique in the set of key-value configuration options. For example,
54
-
both `feature = "std"` and `feature = "serde"` can be set at the same time.
42
+
Keys do not need to be unique. For example, both `feature = "std"` and `feature = "serde"` can be set at the same time.
55
43
56
44
## Set Configuration Options
57
45
58
46
Which configuration options are set is determined statically during the
59
-
compilation of the crate. Certain options are _compiler-set_ based on data
60
-
about the compilation. Other options are _arbitrarily-set_, set based on input
61
-
passed to the compiler outside of the code. It is not possible to set a
47
+
compilation of the crate. Some options are _compiler-set_ based on data
48
+
about the compilation. Other options are _arbitrarily-set_ based on input
49
+
passed to the compiler outside of the code. It is impossible to set a
62
50
configuration option from within the source code of the crate being compiled.
63
51
64
52
> **Note**: For `rustc`, arbitrary-set configuration options are set using the
@@ -70,11 +58,7 @@ configuration option from within the source code of the crate being compiled.
70
58
71
59
<divclass="warning">
72
60
73
-
Warning: It is possible for arbitrarily-set configuration options to have the
74
-
same value as compiler-set configuration options. For example, it is possible
75
-
to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and
76
-
have both `unix` and `windows` configuration options set at the same time. It
77
-
is unwise to actually do this.
61
+
Warning: Arbitrarily-set configuration options can clash with compiler-set configuration options. For example, it is possible to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and have both `unix` and `windows` configuration options set at the same time. Doing this would be unwise.
78
62
79
63
</div>
80
64
@@ -266,9 +250,7 @@ Example values:
266
250
<!-- should we say they're active attributes here? -->
267
251
268
252
The `cfg`[attribute] conditionally includes the thing it is attached to based
269
-
on a configuration predicate.
270
-
271
-
It is written as `cfg`, `(`, a configuration predicate, and finally `)`.
253
+
on the given configuration predicate (_ConfigurationPredicate_ in the syntax above).
272
254
273
255
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
274
256
on it. If the predicate is false, the thing is removed from the source code.
0 commit comments