Skip to main content
A path in Rust is a hierarchical namespace resolution mechanism used to locate items—such as modules, structs, functions, traits, or constants—within the module tree. It consists of a sequence of segments separated by the double-colon path separator (::), where each segment is an identifier that may optionally include generic arguments (e.g., Option<i32> or Vec::<T>::new). Paths are strictly categorized into two types based on their point of origin within the module hierarchy: 1. Absolute Paths Absolute paths originate from a definitive root. They begin with the crate keyword (representing the root of the current crate), the name of an external crate, or the global path prefix ::. The global path prefix is critical for unambiguously specifying an absolute path when a local item shadows an external crate name.
2. Relative Paths Relative paths originate from the current module’s scope. They begin with an identifier present in the local scope, or with the special keywords self or super.
Path Keywords Rust reserves specific keywords to control path traversal:
  • crate: Resolves to the absolute root of the compilation unit.
  • super: Resolves to the immediate parent of the current module. It functions identically to .. in a filesystem and can be chained (e.g., super::super::Item).
  • self: Resolves to the current module. It is primarily used to disambiguate shadowing or to explicitly group module-level items.
  • Self: Resolves to the implementing type within traits and impl blocks (e.g., Self::default()).
Resolution Mechanics When the Rust compiler evaluates a path, it performs sequential segment resolution. Starting from the origin point, the compiler traverses the namespace downward. Each intermediate segment in the path must resolve to an item that contains an inner namespace, such as a module, enum, struct, or trait. For example, in the path String::new, the intermediate segment String resolves to a struct, and in Iterator::Item, the intermediate segment Iterator resolves to a trait. The final segment resolves to the target item itself. During traversal, the compiler strictly enforces privacy boundaries. Every segment accessed via a path must be visible to the module where the path is declared. This does not strictly require the pub modifier; items are implicitly visible to their child modules, and restricted visibility modifiers like pub(crate) or pub(super) can grant access across specific module boundaries without making the intermediate or target items universally public.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More