アトリビュート

アトリビュートはモジュール、クレート、要素に対するメタデータです。以下がその使用目的です。

Attributes look like #[outer_attribute] or #![inner_attribute], with the difference between them being where they apply.

  • #[outer_attribute] applies to the item immediately following it. Some examples of items are: a function, a module declaration, a constant, a structure, an enum. Here is an example where attribute #[derive(Debug)] applies to the struct Rectangle:

    #![allow(unused)]
    fn main() {
    #[derive(Debug)]
    struct Rectangle {
        width: u32,
        height: u32,
    }
    }
  • #![inner_attribute] applies to the enclosing item (typically a module or a crate). In other words, this attribute is interpreted as applying to the entire scope in which it's placed. Here is an example where #![allow(unused_variables)] applies to the whole crate (if placed in main.rs):

    #![allow(unused_variables)]
    
    fn main() {
        let x = 3; // This would normally warn about an unused variable.
    }

アトリビュートは以下の様な書き方で引数を取ることができます。

  • #[attribute = "value"]
  • #[attribute(key = "value")]
  • #[attribute(value)]

アトリビュートは複数の値を取ることができ、複数の行に分割することもできます。

#[attribute(value, value2)]


#[attribute(value, value2, value3,
            value4, value5)]