macro

$crate and cross-crate helper paths

Use $crate to anchor a helper path to the defining crate, not the caller. The helper must still be accessible from the expansion site. See the two-crate fixture for a renamed dependency. Expected stdout (not executed): '12\n' Common mistake: Using $crate does not bypass privacy.

Formal Syntax & Structure

rust syntaxrust
$crate::helper(...)

Working Example

Example Snippetrust
fn fee(n: u32) -> u32 { n + 2 }#[macro_export] macro_rules! with_fee { ($n:expr) => { $crate::fee($n) }; }fn main() { println!("{}", with_fee!(10)); }
Rust Reference