AST HIR and name resolution
Quick answer
An abstract syntax tree (AST) describes parsed source structure. rustc lowers expanded, resolved source into a high-level intermediate representation (HIR) suited to later analysis.
Names and lowering
Name resolution determines which declaration a use refers to; lexical scopes and paths matter. Macro expansion and name resolution interact in the compiler. Lowering simplifies certain surface forms, such as for loops, into more uniform structures. The AST/HIR details are implementation-specific and are not stable serialization contracts.
mod north { pub fn label() -> &'static str { "north" } }mod south { pub fn label() -> &'static str { "south" } }fn main() { println!("{} {}", north::label(), south::label()); }Expected stdout (not executed):
north southIdentical short names can denote distinct items.
fn main() { let mut a = 0; for n in [2, 4, 6] { a += n; } let mut b = 0; let mut iter = [2, 4, 6].into_iter(); while let Some(n) = iter.next() { b += n; } println!("{a} {b}");}Expected stdout (not executed):
12 12The second loop illustrates an iteration mechanism, not the exact HIR lowering.
Common mistake
Do not rely on private compiler node IDs or assume this pedagogical rewrite is exact for every temporary lifetime.
Try it yourself
Add a local binding that shadows a module name and use an explicit crate path to reach the module item.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
AST HIR and name resolution — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Two modules export a function named label. What distinguishes their calls?
AST HIR and name resolution — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust mod north { pub fn label() -> &'static str { "north" } } mod south { pub fn label() -> &'static str { "south" } } fn main() { println!("{} {}", north::label(), south::label()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
AST HIR and name resolution — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let mut a = 0; for n in [2, 4, 6] { a += n; } let mut b = 0; let mut iter = [2, 4, 6].into_iter(); while let Some(n) = iter.next() { b += n; } println!("{a} {b}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.