アトリビュート
アトリビュートはモジュール、クレート、要素に対するメタデータです。以下がその使用目的です。
- コンパイル時の条件分岐
- クレート名、バージョン、種類(バイナリか、ライブラリか)の設定
- リントの無効化
- コンパイラ付属の機能(マクロ、グロブ、インポートなど)の使用
- 外部ライブラリへのリンク
- ユニットテスト用の関数を明示
- ベンチマーク用の関数を明示
- アトリビュートマクロ
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 structRectangle:#![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 inmain.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)]