Macro doesn't see variable

Why the get_time! macro doesn't see the val variable? They are in the same file. I do not want to make that var variable public so it can be use only by that macro

static val: OnceLock = OnceLock::new();

#[macro_export]
macro_rules! get_time {
() =>{ val.set(Instant::now());}
}

A macro is expanded at the call site, so any items that are referred to by the macro must be visible at the call site. If you want to call your macro anywhere else other than the module[1] val is defined in, it needs to be public to the extend that it is accessible from that location. You can add a #[doc(hidden)] to val to keep it from appearing in your public documentation.


  1. Or one of its submodules. ↩ī¸Ž

2 Likes