liveBook · Manning

Absolute path

As explained in Refactoring to Rust

An absolute path is a Rust path resolved from a crate or other root namespace.

An absolute path identifies a Rust item from a crate root. External paths begin with a crate name, such as std::io::stdin; paths into the current crate begin with crate. section 5.2.1 section 5.4

Use absolute paths when accessing items across a module structure:

  • Write crate::module::Item to reach an item from the current crate root. section 5.2.1
  • Write an external crate name, such as std::io::stdin, to reach an item in another crate. section 5.2.1
  • Expose modules or items with pub before accessing them from another module. section 5.2.1
  • Shorten repeated absolute paths with use, as in use crate::treats::Treat;. section 5.2.1

The same snack API with absolute paths

Within a nested shop module, buy can return crate::treats::Treat and construct crate::treats::Treat::IceCream. The eat function can accept crate::treats::Treat and return crate::treats::ConsumedTreat, while regret can accept crate::treats::ConsumedTreat. These paths remain rooted at the crate rather than at the current module.

How Refactoring to Rust covers it

How deep it goes
Explains crate roots, crate, pub, and use through parallel module examples, but stops short of deeper namespace mechanics.
Examples
Uses std::io::stdin, a libsnack module tree, lifecycle functions, and use crate declarations.
What it assumes
Assumes basic Rust items and namespaces while introducing modules, visibility, and crate-root addressing incrementally.
What you can do after
Apply it via Rust module paths to reference current-crate and external-crate items.
How it connects
Connects crates, modules, relative paths, visibility, and use declarations.

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 Refactoring to Rust. The section links point to the parts of the book it draws on.