Attributes
Syntax
// apply to the whole crate
#![crate_attribute]
// apply to to a module or item
#[item_attribute]
#[attribute = "value"]
#[attribute(key = "value")]
#[attribute(value)]
#[attribute(value, value2)]
#[attribute(value, value2, value3,
value4, value5)]
Common Attributes
allow
// disables the `dead_code` lint
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(unconditional_panic, unused_must_use)]
cfg
// the cfg attribute: #[cfg(...)] in attribute position
// the cfg! macro: cfg!(...) in boolean expressions
// This function only gets compiled if the target OS is linux
#[cfg(target_os = "linux")]
// And this function only gets compiled if the target OS is *not* linux
#[cfg(not(target_os = "linux"))]
// same as this
if cfg!(target_os = "linux") {
println!("Yes. It's definitely linux!");
}
- Common
#[cfg(...)] options
test
debug_assertions
unix and windows
target_pointer_width = "64" // other value is 32
target_arch = "x86_64" // “x86”, “arm”, “aarch64” etc.
target_os = "macos" // “windows”, “ios”, “android”, “linux”, “freebsd”, “openbsd” etc.
feature= "robots" // user-defined feature declared in [features] section in Cargo.toml
not(A) // #[cfg(X)] and #[cfg(not(X))]
all(A,B) // both A and B satisfied
any(A,B) // either or
inline
// inline expansion of functions
#[inline]