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.

A use alias in Rust is a module system feature that binds an imported item to a new, local identifier within the current scope using the as keyword. This creates a local namespace binding without altering the original item’s canonical name or definition.
use original::path::Item as LocalAlias;

Mechanics of Aliasing

Scope and Resolution The alias exists exclusively within the lexical scope of the use declaration. During the compiler’s name resolution phase, any reference to the alias is statically resolved to the original fully qualified path. Supported Item Types The as keyword can alias any valid Rust item brought into scope, including:
  • Modules
  • Structs, Enums, and Unions
  • Traits
  • Functions and Constants
  • Macros

Syntax Variations

Grouped Imports Aliases can be declared within nested import trees using curly braces {}. Each item in the group can be individually aliased alongside non-aliased imports.
use std::collections::{
    HashMap as Map,
    HashSet as Set,
    BTreeMap,
};
Module Aliasing Entire modules can be aliased. This establishes an alternative local identifier for the module’s path. The original absolute path remains valid and accessible; the alias does not shadow, hide, or invalidate the original canonical path.
use std::io as standard_io;

fn read_input() -> standard_io::Result<()> {
    let mut buffer = String::new();
    // The original absolute path remains perfectly valid
    std::io::stdin().read_line(&mut buffer)?;
    Ok(())
}
Re-exporting with Aliases When combined with a visibility modifier (e.g., pub, pub(crate)), the alias becomes part of the module’s exported API. External modules consuming this module will interact with the item exclusively via the aliased identifier.
mod internal {
    pub struct ComplexInternalName;
}

// Re-exports the struct under a new identifier
pub use internal::ComplexInternalName as PublicName;
Aliasing External Crates Crate-level use statements can utilize aliases to rename the root namespace of an external dependency within the current crate.
use futures_util as futures;
The _ (Underscore) Alias The Rust compiler allows any item (structs, functions, modules, traits, etc.) to be aliased to the underscore character _. This brings the item into scope without introducing a named identifier into the local namespace. Idiomatically, this is most commonly used for traits to satisfy the compiler’s trait resolution requirements, enabling the trait’s methods to be called on implementing types without polluting the local scope.
use std::io::Write as _;

fn example() {
    let mut stdout = std::io::stdout();
    // .write_all() is available because the Write trait is in scope via `_`
    stdout.write_all(b"Hello").unwrap(); 
}
Master Rust with Deep Grasping Methodology!Learn More