liveBook · Manning

Absolute path

As explained in Write Powerful Rust Macros

A full path is a Rust path that identifies an item through its complete namespace qualification.

An absolute path identifies a Rust item through its complete namespace qualification. In generated code, full paths keep references independent of the user’s imports and local names. section 10.1.3

Use absolute paths in generated Rust code because they:

  • Resolve items unavailable through the Rust prelude, such as std::collections::HashMap, without requiring user imports. section 10.1.3
  • Avoid collisions when application code defines another item with the same name, including types or traits such as HashMap and Default. section 10.1.3 section 8.4
  • Hide implementation choices so generated APIs can change their underlying types without forcing application-code changes. section 10.1.3

Qualifying generated types and traits

quote! { pub struct Config( pub std::collections::HashMap<&'static str, &'static str> ); } quote_spanned! {ty.span()=> struct ExampleStruct where SomeType: core::default::Default; } section 10.1.3 section 8.4

error[E0412]: cannot find type `HashMap` in this scope
 --> config-macro-usage/src/main.rs:5:5
  |
5 |     config!();
  |     ^^^^^^^^^ not found in this scope
  |

Inspect the compiler failure produced when generated code refers to HashMap without a qualifying path. section 10.1.3

How Write Powerful Rust Macros covers it

How deep it goes
Explains missing imports, name collisions, and implementation hiding, then demonstrates qualified types and trait bounds in generated code.
Examples
Demonstrates std::collections::HashMap, core::default::Default, quote!, quote_spanned!, and compiler errors from unresolved or colliding names.
What it assumes
Assumes Rust paths, imports, procedural macros, quote!, and trait bounds; teaches why generated code cannot rely on caller scope.
What you can do after
Build it from scratch by qualifying dependencies in generated Rust code and reducing import, collision, and implementation-hiding problems.
How it connects
Connects absolute paths with Rust macros, the prelude, name collisions, implementation details, traits, spans, and generated code.

Also explained in

Compare how 2 books explain absolute path →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.