11.3 cfg

조건부 컴파일은 두 개의 다른 연산자를 통해 가능하다.

  • cfg 속성: 속성 위치에 #[cfg(...)]
  • cfg! 매크로: boolean 식에서 cfg!(...)

둘은 동일한 인자 구문을 이용한다.

// OS linux .
#[cfg(target_os = "linux")]
fn are_you_on_linux() {
println!("You are running linux!")
}
// OS linux .
#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
println!("You are *not* running linux!")
}
fn main() {
are_you_on_linux();
println!("Are you sure?");
if cfg!(target_os = "linux") {
println!("Yes. It's definitely linux!");
} else {
println!("Yes. It's definitely *not* linux!");
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

See also:

the reference, cfg!, and macros.

results matching ""

    No results matching ""