Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The use declaration in Rust creates one or more local name bindings to items defined in a specific module path. While it eliminates the need to specify the fully qualified path for structs, enums, and functions, bringing a trait into scope via use is strictly required to access its methods using standard method-call syntax (obj.method()). In modern Rust (2018 edition and later), paths in use declarations resolve similarly to standard paths: they are relative to the current module scope by default, or they resolve to external crates available in the extern prelude. Absolute paths typically begin with crate:: or a specific crate name, but they can also begin with a leading :: (e.g., ::std::collections::HashMap). This leading :: explicitly denotes an absolute path, which is sometimes necessary to disambiguate an external crate from a local module sharing the same name.

Path Resolution

Rust provides specific keywords and syntax to dictate how the module tree is traversed when resolving a use path:
// Resolves to an external crate in the extern prelude
use std::collections::HashMap;

// Explicitly absolute path, bypassing any local module named `std`
use ::std::collections::HashMap as AbsoluteHash;

pub mod network {
    pub struct Connection;
}

pub mod configuration {
    pub struct Config;
}

mod child_module {
    // Absolute path from the root of the current crate
    use crate::network::Connection;

    // Relative path from the parent module
    use super::configuration::Config;

    pub mod internal {
        pub struct Helper;
    }

    // Explicit relative path from the current module
    use self::internal::Helper;

    // Implicit relative path from the current module scope
    use internal::Helper as ImplicitHelper;
}

Aliasing and Anonymous Imports

The as keyword binds the imported item to a new local identifier. This is strictly a compile-time renaming mechanism used to resolve namespace collisions or to disambiguate items with identical names from different modules. Additionally, the _ identifier can be used to create an anonymous import. This is a critical pattern for traits: it brings a trait’s methods into scope for method resolution without binding the trait’s name to the local namespace, thereby preventing namespace pollution.
use std::fmt::Result;
use std::io::Result as IoResult;

// Anonymous import: brings Read methods into scope without binding the name `Read`
use std::io::Read as _;

Nested Paths

To reduce boilerplate, multiple items sharing a common path prefix can be grouped using curly braces {}. The self keyword can be included within the braces to import the common prefix itself alongside its sub-items.
// Imports both BTreeMap and HashSet from std::collections
use std::collections::{BTreeMap, HashSet};

// Imports std::io and std::io::Write
use std::io::{self, Write};

Glob Imports

The glob operator * brings all public items defined in a module, or all variants of an enum, into the current scope. When applied to a module, this creates bindings for every public struct, enum, trait, function, constant, static, type alias, macro, and submodule within the target module. When applied to an enum, it brings all of its variants directly into the local namespace.
// Imports all public items (structs, macros, etc.) from the module
use std::collections::*;

enum Status {
    Active,
    Inactive,
    Pending,
}

// Brings Active, Inactive, and Pending into the current scope
use Status::*;

Re-exporting (Visibility)

Bindings created by use are private to the module they are declared in by default. Applying a visibility modifier to a use declaration creates a public or semi-public binding, effectively re-exporting the item. This allows external code to access the item through the current module’s path rather than its original definition path. Restricted visibility modifiers like pub(crate) use or pub(super) use are commonly used to expose items only to specific scopes within the crate hierarchy without making them fully public.
mod internal {
    pub struct HiddenStruct;
    pub struct CrateStruct;
}

// Re-exports HiddenStruct to the public API of the current module
pub use internal::HiddenStruct;

// Re-exports CrateStruct only to the rest of the current crate
pub(crate) use internal::CrateStruct;

Lexical Scoping

use declarations obey standard lexical scoping rules. When declared inside a block (such as a function body), the name binding is strictly limited to that block and shadows any identical names in the outer scope. Note that use statements are not valid associated items and cannot be placed directly inside an impl block, though they can be used freely inside the methods defined within an impl block.
const PI: f32 = 3.14;

fn calculate_precision() {
    // This binding is only valid within calculate_precision()
    // and shadows the outer f32 PI.
    use std::f64::consts::PI;
}
Master Rust with Deep Grasping Methodology!Learn More