36

Rust has the ability to check configuration at build time with, e.g., #[cfg(target_os = "linux")] or if cfg!(target_os = "linux") {...}, where target_os is a feature.

Is there a list of all (or, at least, commonly used) features that can be checked in Rust?


See related question regarding attributes Is there an exhaustive list of standard attributes anywhere?.

nbro
  • 15,395
  • 32
  • 113
  • 196
ideasman42
  • 42,413
  • 44
  • 197
  • 320

3 Answers3

54

The "Conditional compilation" section of the Reference has a list of configurations that must be defined (as of Rust 1.14):

  • target_arch with values like:
    • x86
    • x86_64
    • mips
    • powerpc
    • powerpc64
    • arm
    • aarch64
  • target_os with values like:
    • windows
    • macos
    • ios
    • linux
    • android
    • freebsd
    • dragonfly
    • bitrig
    • openbsd
    • netbsd
  • target_family with values like:
    • unix
    • windows
  • unix (shortcut for target_family)
  • windows (shortcut for target_family)
  • target_env with values like:
    • gnu
    • msvc
    • musl
    • "" (empty string)
  • target_endian with values:
    • little
    • big
  • target_pointer_width with values like:
    • 32
    • 64
  • target_has_atomic with values like:
    • 8
    • 16
    • 32
    • 64
    • ptr
  • target_vendor with values like:
    • apple
    • pc
    • unknown
  • test
  • debug_assertions
lifeless
  • 4,045
  • 1
  • 17
  • 6
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
9

You can also use this command: rustc --print target-list.

Each triple are formatted as follows: {arch}-{vendor}-{sys}-{abi}.

For example, the triple 'arm-unknown-linux-gnueabihf' refers to:

  1. architecture: arm
  2. vendor: unknown. In this case, no vendor was specified.
  3. system: linux
  4. ABI: gnueabihf
Bo Lu
  • 687
  • 9
  • 11
  • 2
    This is helpful to know, but not useful in constructing `cfg` attributes, as system does not necessarily pertain to `target_os` (etc.; for example, system is `"darwin"` but `target_os` needs to be specified as `"macos"`). – Jake Ireland Nov 15 '22 at 10:21
3

See also https://internals.rust-lang.org/t/all-the-rust-features/4322 for a comprehensive list of features.

Bear in mind that some / most of the features won't be stabilized so will only be available in nightly for some time and are subject to breaking improvements / upgrades until they are either stabilized or discontinued.

Features in rust nightly is survival of the fittest.

Squirrel
  • 1,189
  • 12
  • 15