Sub topic 3
Paths and use declarations
This chapter assumes you can write functions and structs, and that you know a module is a named group of items, as covered in the previous chapter. It explains how to name an item from another place in the module tree, and how use shortens those names.
Paths name items
A path is a list of names separated by :: that leads to an item such as a function, struct, or module. Paths come in two forms:
- An absolute path starts at the crate root with
crate::, or at an external crate such asstd::. - A relative path starts in the current module. It begins with a name in scope,
self::for the current module, orsuper::for the parent module.
The program below uses each kind of path.
fn announce(msg: &str) {
println!("{msg}");
}
mod garden {
pub mod veg {
pub fn grow() -> &'static str {
super::label();
"carrot"
}
}
fn label() {
crate::announce("growing");
}
pub fn tend() -> &'static str {
self::veg::grow()
}
}
fn main() {
println!("{}", crate::garden::tend());
println!("{}", garden::veg::grow());
}In grow, super::label() is a relative path to the parent module. In label, crate::announce is an absolute path. In main, garden::veg::grow() is a relative path, because garden is a child of the root module that main lives in.
Use an absolute path when the item is unlikely to move relative to the root. Use a relative path when the caller and the item are likely to move together.
Shortening paths with use
A use declaration binds a short name to a path for the scope it appears in. It does not change who is allowed to access the item.
mod garden {
pub mod veg {
pub fn grow() -> &'static str {
"carrot"
}
}
}
use garden::veg;
use std::collections::HashMap as Map;
fn main() {
println!("{}", veg::grow());
let mut stock: Map<&str, u32> = Map::new();
stock.insert("carrot", 3);
println!("{:?}", stock);
}Several forms are common:
| Form | Effect |
|---|---|
| `use a::b;` | Brings `b` into scope |
| `use a::b as c;` | Brings `b` in under the name `c` |
| `use a::{b, c};` | Brings in several items from one parent |
| `use a::{self, b};` | Brings in `a` itself and `b` |
| `use a::*;` | Brings in every public item of `a` (glob import) |
Idiomatic style
- For functions, import the parent module and call
veg::grow(). The call site then shows where the function comes from. - For structs, enums, and traits, import the item itself:
use std::collections::HashMap;. - When two items share a name, import their parent modules or rename one with
as. For example,std::fmt::Resultandstd::io::Resultcollide if both are imported directly. - Avoid globs outside tests and preludes. They hide where names come from, and a new public item added upstream can cause a name clash.
Common errors
- E0433, "failed to resolve: use of undeclared crate or module". The first segment of the path is not in scope. Check for a missing
use, or addcrate::orsuper::. - E0603, "function is private". The path is correct, but the item is not
pub. Privacy is covered in the next chapter. - Unused import warning. Remove the
use, or use the name.
Exercise
In the garden example, a new module garden::shed contains a function store. It must call garden::tend. Write the call using a super:: path, then write it using a crate:: path.
Show answer
With super: super::tend(), because shed is a child of garden and its parent is garden. With crate: crate::garden::tend(). Both work, but the super:: form keeps working if garden moves elsewhere in the tree along with shed.