use & Paths
use brings paths into scope. Re-export with pub use. Prefer explicit imports over glob in application code.
Recipe
use std::collections::HashMap;
use crate::config::Settings;
pub use crate::error::Error;When to reach for this: Reducing path noise and crafting public crate API surface.
Working Example
mod prelude {
pub use super::{Api, Client};
}
pub struct Api;
pub struct Client;
pub mod prelude;What this demonstrates:
pub usereexports for ergonomic imports- Prelude module pattern
crate::absolute path from crate root
Deep Dive
Paths: crate::, super::, self::, external serde::. use a::{b, c} grouped. as rename: use std::fmt::Display as FmtDisplay.
Gotchas
- Glob
use std::collections::*- Namespace pollution. Fix: Explicitusein libs. - Ambiguous glob - Compile error on conflict. Fix: Rename.
- pub use semver - Reexport is public API commitment.
- deep relative paths - Fragile moves. Fix:
crate::paths. - unused imports - rustfix / clippy.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| Fully qualified path | One-off | Repeated use |
| Prelude module | Common imports | Tiny crate |
#[rustfmt::skip] imports | Grouping style | Default fmt ok |
FAQs
self vs crate?
self current module tree; crate root.super?
Parent module.extern crate?
2018 edition implicit.use type alias?
pub type Result<T> = ...nested use?
use std::{io, io::Read};reexport macro?
pub use macro_rules path tricky - macro_export.rust-analyzer?
Auto import assists.edition 2024 paths?
Check migration for path changes.wasm?
Same module rules.test use?
tests import crate with use crate::.Related
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.