use, super, self
Un módulo puede incluir símbolos de otro módulo en el ámbito con use. Normalmente, se ve algo como esto en la parte superior de cada módulo:
use std::collections::HashSet; use std::process::abort;
Rutas
Las rutas se resuelven de la siguiente manera:
-
Como ruta relativa:
foooself::foohacen referencia afooen el módulo.super::foohace referencia afooen el módulo superior.
-
Como ruta absoluta:
crate::foohace referencia afooen la raíz del crate.bar::foohace referencia afooen el cratebar.
-
It is common to “re-export” symbols at a shorter path. For example, the top-level
lib.rsin a crate might havemod storage; pub use storage::disk::DiskStorage; pub use storage::network::NetworkStorage;making
DiskStorageandNetworkStorageavailable to other crates with a convenient, short path. -
For the most part, only items that appear in a module need to be
use’d. However, a trait must be in scope to call any methods on that trait, even if a type implementing that trait is already in scope. For example, to use theread_to_stringmethod on a type implementing theReadtrait, you need touse std::io::Read. -
The
usestatement can have a wildcard:use std::io::*. This is discouraged because it is not clear which items are imported, and those might change over time.