Rust :: Attributes

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!");
    }

inline

// inline expansion of functions
#[inline]