From c299ce497b0656e6cee393a599055090222de376 Mon Sep 17 00:00:00 2001 From: bohan Date: Thu, 28 Aug 2025 01:05:46 +0800 Subject: [PATCH] local variables has higher priority than item --- src/macros-by-example.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/macros-by-example.md b/src/macros-by-example.md index b2552054d..4f6d6053d 100644 --- a/src/macros-by-example.md +++ b/src/macros-by-example.md @@ -439,6 +439,30 @@ m!(define); m!(refer); ``` +And local variables has higher priority than items: + +```rust +// example 1: +let f0 = || 42; +fn f0() -> i32 { 8 } + +macro_rules! m0 { + () => { f0() } +} + +assert_eq!(m0!(), 42); + +// example 2: +fn f1() -> i32 { 8 } +let f1 = || 42; + +macro_rules! m1 { + () => { f1() } +} + +assert_eq!(m1!(), 42); +``` + r[macro.decl.hygiene.crate] A special case is the `$crate` metavariable. It refers to the crate defining the macro, and can be used at the start of the path to look up items or macros which are not in scope at the invocation site.