keyword

Rust crate: refer to the current crate root

The `crate` path prefix starts at the current crate root, even inside a nested module. A crate is a compilation unit; it is not necessarily an entire Cargo package. Expected output (not executed): 24 Why: The nested module calls a function defined at the root. Common mistake: `crate::` does not refer to an arbitrary external dependency.

Formal Syntax & Structure

rust syntaxrust
crate::module::item; pub(crate) item

Working Example

Example Snippetrust
fn base_price() -> u32 { 8 }
mod shop {    pub fn total() -> u32 { crate::base_price() * 3 }}
fn main() {    println!("{}", shop::total());}
Rust Reference