macro

Spans and call-site name resolution

Spans carry source and expansion context. An identifier constructed with the call-site span can resolve a name in the consumer scope. The example deliberately requires a local variable named local_score; this is a teaching case, not a recommended implicit API. Example: two-crate workspace; see fixtures/proc-macro-workspace/consumer/src/bin/span.rs. This caller is not a standalone rustc program. Expected stdout (not executed): '31\n' Common mistake: Assuming a span is just a line number or that every identifier resolves in the macro crate.

Formal Syntax & Structure

rust syntaxrust
Span::call_site()

Working Example

Example Snippetrust
use teaching_macros::caller_name;fn main() { let local_score = 31; println!("{}", caller_name!()); }
Rust Reference