module-concept

Rust Relative paths with self and super

Use an explicit local or parent path when related modules should move together. Repeated super segments climb one ancestor at a time; they cannot go above the root. Expected result (not executed): 12 Why: self resolves bins from warehouse; super resolves BASE from bins’ parent. Common mistake: Confusing self in a path with the method receiver.

Formal Syntax & Structure

rust syntaxrust
self::child::item; super::item

Working Example

Example Snippetrust
mod warehouse {    const BASE: u32 = 10;    mod bins { pub(super) fn count() -> u32 { super::BASE + 2 } }    pub fn count() -> u32 { self::bins::count() }}fn main() { println!("{}", warehouse::count()); }
Rust Reference