macro_rules! once { ($exp:expr) => { once!((), $exp) }; ($type:ty, $exp:expr) => {{ static ONCE: once_cell::sync::OnceCell<$type> = once_cell::sync::OnceCell::new(); ONCE.get_or_init(|| $exp) }}; } fn main() { // If you run this, you can see that the expressions are executed only once. once(); once(); once(); assert_eq!(42, calc()); assert_eq!(42, calc()); assert_eq!(42, calc()); } fn once() { once!(println!("Doing it only once")); } fn calc() -> u8 { *once!(u8, { println!("Calculating the value"); 3 * 2 * 7 * 5 / 4 - 10 }) }