[][src]Macro if_rust_version::if_rust_version

macro_rules! if_rust_version {
    (if rust_version $($tail:tt)*) => { ... };
    ($op:tt $n:tt { $($if_:tt)* }) => { ... };
    ($op:tt $n:tt { $($if_:tt)* } else { $($else_:tt)* }) => { ... };
    ($op:tt $n:tt { $($if_:tt)* } else if rust_version $($tail:tt)*) => { ... };
}

This macro can enable or disable code depending on the rust version with which the program is compiled.

The syntax is this (pseudo-code): (if rust_version)? <operator> <version> { <code> } (else if rust_version <operator> <version> { <code> })* (else { <code> })?

The operator is one of ==, !=, >=, <=, < or >. The version is either nightly or a version number in the form 1.x.

Important: The version number can only have one period, and start with 1.. So for example simply 1.36, but NOT 1.36.0 or 0.42

The macro will expand to the code corresponding to the right condition. (Or nothing if no condition match).

Examples:

if_rust_version!{ == nightly {
    fn foo() { /* implementation on nightly */ }
} else if rust_version >= 1.33 {
    fn foo() { /* implementation on rust 1.33 or later */ }
} else {
    fn foo() { /* implementation on rust 1.33 on old rust */ }
}}
if_rust_version!{ >= 1.36 { use std::mem::MaybeUninit; }}
// ...
if_rust_version!{ < 1.36 {
    let mut foo: u32 = unsafe { ::std::mem::uninitialized() };
} else {
    let mut foo: u32 = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
}}

Note that in case this is used as an expression, no blocks will be added.

// Error
println!("{}", if_rust_version!{ < 1.22 { let x = 42; x} else { 43 } } );
// OK
println!("{}", { if_rust_version!{ < 1.22 { let x = 42; x} else { 43 } } } );
// Also OK
println!("{}", if_rust_version!{ < 1.22 { {let x = 42; x} } else { 43 } }  );