Macro write_const

Source
macro_rules! write_const {
    ($id:ident, $t:ty, $data:expr) => { ... };
}
Expand description

Write a constant variable.

Makes the constant available for import into the main crate via use_symbols.

§Parameters

  • $id: the name of the constant. This must be used when importing with use_symbols.
  • $t: the type of the constant.
  • $data: the data to assign to the constant. Must be representable on the stack.

§Example

build.rs

use rustifact::ToTokenStream;

fn main() {
   let meaning_of_life = Some(42);
   rustifact::write_const!(MEANING_OF_LIFE, Option<i32>, meaning_of_life);
}

src/main.rs

rustifact::use_symbols!(MEANING_OF_LIFE);
// The above line is equivalent to the declaration:
// const MEANING_OF_LIFE: Option<i32> = Some(42);

fn main() {
    if let Some(mean) = MEANING_OF_LIFE {
        println!("The meaning of life is {}", mean);
    } else {
        println!("Life has no meaning.");
    }
}