@@ -230,101 +230,9 @@ See [The Rustdoc Book] for reference material on this attribute.
230
230
231
231
### Conditional compilation
232
232
233
- Sometimes one wants to have different compiler outputs from the same code,
234
- depending on build target, such as targeted operating system, or to enable
235
- release builds.
236
-
237
- Configuration options are boolean (on or off) and are named either with a
238
- single identifier (e.g. ` foo ` ) or an identifier and a string (e.g. ` foo = "bar" ` ;
239
- the quotes are required and spaces around the ` = ` are unimportant). Note that
240
- similarly-named options, such as ` foo ` , ` foo="bar" ` and ` foo="baz" ` may each be
241
- set or unset independently.
242
-
243
- Configuration options are either provided by the compiler or passed in on the
244
- command line using ` --cfg ` (e.g. ` rustc main.rs --cfg foo --cfg 'bar="baz"' ` ).
245
- Rust code then checks for their presence using the ` #[cfg(...)] ` attribute:
246
-
247
- ``` rust
248
- // The function is only included in the build when compiling for macOS
249
- #[cfg(target_os = " macos" )]
250
- fn macos_only () {
251
- // ...
252
- }
253
-
254
- // This function is only included when either foo or bar is defined
255
- #[cfg(any(foo, bar))]
256
- fn needs_foo_or_bar () {
257
- // ...
258
- }
259
-
260
- // This function is only included when compiling for a unixish OS with a 32-bit
261
- // architecture
262
- #[cfg(all(unix, target_pointer_width = " 32" ))]
263
- fn on_32bit_unix () {
264
- // ...
265
- }
266
-
267
- // This function is only included when foo is not defined
268
- #[cfg(not(foo))]
269
- fn needs_not_foo () {
270
- // ...
271
- }
272
- ```
273
-
274
- This illustrates some conditional compilation can be achieved using the
275
- ` #[cfg(...)] ` attribute. ` any ` , ` all ` and ` not ` can be used to assemble
276
- arbitrarily complex configurations through nesting.
277
-
278
- The following configurations must be defined by the implementation:
279
-
280
- * ` target_arch = "..." ` - Target CPU architecture, such as ` "x86" ` ,
281
- ` "x86_64" ` ` "mips" ` , ` "powerpc" ` , ` "powerpc64" ` , ` "arm" ` , or
282
- ` "aarch64" ` . This value is closely related to the first element of
283
- the platform target triple, though it is not identical.
284
- * ` target_os = "..." ` - Operating system of the target, examples
285
- include ` "windows" ` , ` "macos" ` , ` "ios" ` , ` "linux" ` , ` "android" ` ,
286
- ` "freebsd" ` , ` "dragonfly" ` , ` "bitrig" ` , ` "openbsd" ` or
287
- ` "netbsd" ` . This value is closely related to the second and third
288
- element of the platform target triple, though it is not identical.
289
- * ` target_family = "..." ` - Operating system family of the target, e. g.
290
- ` "unix" ` or ` "windows" ` . The value of this configuration option is defined
291
- as a configuration itself, like ` unix ` or ` windows ` .
292
- * ` unix ` - See ` target_family ` .
293
- * ` windows ` - See ` target_family ` .
294
- * ` target_env = ".." ` - Further disambiguates the target platform with
295
- information about the ABI/libc. Presently this value is either
296
- ` "gnu" ` , ` "msvc" ` , ` "musl" ` , or the empty string. For historical
297
- reasons this value has only been defined as non-empty when needed
298
- for disambiguation. Thus on many GNU platforms this value will be
299
- empty. This value is closely related to the fourth element of the
300
- platform target triple, though it is not identical. For example,
301
- embedded ABIs such as ` gnueabihf ` will simply define ` target_env ` as
302
- ` "gnu" ` .
303
- * ` target_endian = "..." ` - Endianness of the target CPU, either ` "little" ` or
304
- ` "big" ` .
305
- * ` target_pointer_width = "..." ` - Target pointer width in bits. This is set
306
- to ` "32" ` for targets with 32-bit pointers, and likewise set to ` "64" ` for
307
- 64-bit pointers.
308
- * ` target_has_atomic = "..." ` - Set of integer sizes on which the target can perform
309
- atomic operations. Values are ` "8" ` , ` "16" ` , ` "32" ` , ` "64" ` and ` "ptr" ` .
310
- * ` target_vendor = "..." ` - Vendor of the target, for example ` apple ` , ` pc ` , or
311
- simply ` "unknown" ` .
312
- * ` test ` - Enabled when compiling the test harness (using the ` --test ` flag).
313
- * ` debug_assertions ` - Enabled by default when compiling without optimizations.
314
- This can be used to enable extra debugging code in development but not in
315
- production. For example, it controls the behavior of the standard library's
316
- ` debug_assert! ` macro.
317
-
318
- You can also set another attribute based on a ` cfg ` variable with ` cfg_attr ` :
319
-
320
- ``` rust,ignore
321
- #[cfg_attr(a, b)]
322
- ```
323
-
324
- This is the same as ` #[b] ` if ` a ` is set by ` cfg ` , and nothing otherwise.
325
-
326
- Lastly, configuration options can be used in expressions by invoking the ` cfg! `
327
- macro: ` cfg!(a) ` evaluates to ` true ` if ` a ` is set, and ` false ` otherwise.
233
+ The ` cfg ` and ` cfg_attr ` attributes control conditional compilation of [ items]
234
+ and attributes. See the [ conditional compilation] section for reference material
235
+ on these attributes.
328
236
329
237
### Lint check attributes
330
238
@@ -570,4 +478,6 @@ You can implement `derive` for your own type through [procedural macros].
570
478
[ modules ] : items/modules.html
571
479
[ statements ] : statements.html
572
480
[ match expressions ] : expressions/match-expr.html
573
- [ external blocks ] : items/external-blocks.html
481
+ [ external blocks ] : items/external-blocks.html
482
+ [ items ] : items.html
483
+ [ conditional compilation ] : conditional-compilation.html
0 commit comments