Attributes
https://doc.rust-lang.org/book/attributes.html
#[hoge]
という記法をattribute
というものらしい。
#[cfg(target_os = "macos")] // macosだけで動作する fn only() { println!("mac"); } #[cfg(target_os = "linux")] // linuxだけで動作する fn only() { println!("linux"); } #[test] // cargo testで動作する fn hoge() { only(); } fn main() { println!("Hello, world!"); only(); }
$ cargo test Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Running target/debug/4_27_attributes-d9c4b09d37979797 running 1 test test hoge ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured $ cargo run Compiling 4_27_attributes v0.1.0 (file:///Users/miura/work/git-work/exercises/The_Rust_Programming_Language/4_27_attributes) Finished debug [unoptimized + debuginfo] target(s) in 0.31 secs Running `target/debug/4_27_attributes` Hello, world! mac