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 * symbol in Rust functions primarily as the unary dereference operator to access the value stored at a memory address, and as the binary multiplication operator for arithmetic operations. It also serves as a syntactic token for defining raw pointer types and executing glob imports.

Unary Dereference Operator

As a prefix unary operator (*expr), it resolves a reference, smart pointer, or raw pointer to its underlying value. Memory Mechanics: When applied to a standard reference (&T or &mut T), * yields the value of type T. If T implements the Copy trait, dereferencing produces a copy of the value. If T is non-Copy, moving the value out of the reference is strictly forbidden by the borrow checker. Instead, dereferencing a non-Copy reference is typically used to read its fields (requiring ref or ref mut bindings to avoid moves during destructuring), or to overwrite the value in place via a mutable reference (e.g., *r = new_val). This assignment overwrites the memory in place and drops the old value; it does not move the old value out. The Box<T> Exception: Because Box<T> is a special compiler built-in (#[lang = "owned_box"]), it is the only pointer type in Rust that allows moving a non-Copy value completely out of it via dereferencing. This is a unique exception to the standard dereferencing rules.
// Overwriting via mutable reference (no move occurs)
let mut x = String::from("A");
let r = &mut x;
*r = String::from("B"); 

// Moving out of a Box (unique compiler exception)
let boxed_string = Box::new(String::from("Hello"));
let unboxed_string: String = *boxed_string; // Value is moved out of the heap allocation
Trait Resolution: For custom types and smart pointers (e.g., Rc<T>, String), the * operator is syntactic sugar for the std::ops::Deref and std::ops::DerefMut traits. The compiler automatically expands *x to *std::ops::Deref::deref(&x) (or *std::ops::DerefMut::deref_mut(&mut x)). The trait method itself returns a standard reference (&Target), and the outer * operator performs the actual dereference of that returned reference.
// Standard reference dereferencing
let x: i32 = 5;
let y: &i32 = &x;
let z: i32 = *y; 

// Smart pointer dereferencing (invokes Deref trait)
let rc_ptr = std::rc::Rc::new(10);
let val: i32 = *rc_ptr; // Expands to: *std::ops::Deref::deref(&rc_ptr)
Unsafe Dereferencing: When applied to raw pointers (*const T or *mut T), the * operator bypasses Rust’s compile-time memory safety guarantees. Because the compiler cannot verify the validity of the memory address, this operation must be explicitly scoped within an unsafe block.
let address: i32 = 42;
let raw_ptr: *const i32 = &address as *const i32;

unsafe {
    let val: i32 = *raw_ptr;
}

Binary Multiplication Operator

As an infix binary operator (expr1 * expr2), it performs arithmetic multiplication between two operands. Trait Resolution: The operation is backed by the std::ops::Mul trait. The compiler translates a * b into std::ops::Mul::mul(a, b). The compound assignment variant (*=) is backed by the std::ops::MulAssign trait.
// Primitive binary multiplication
let a: i32 = 4 * 5;

// Custom type implementation
struct Vector2D(f64, f64);

impl std::ops::Mul<f64> for Vector2D {
    type Output = Self;
    
    fn mul(self, scalar: f64) -> Self::Output {
        Vector2D(self.0 * scalar, self.1 * scalar)
    }
}

Type Signature and Path Syntax

Beyond expression evaluation, the * token is utilized in two specific syntactic constructs: Raw Pointer Types: Used in type signatures to denote immutable (*const T) or mutable (*mut T) raw pointers. It is part of the type name, not an operator in this context.
let ptr: *mut u8;
Glob Imports: Used in use declarations as a wildcard to bind all public items of a module into the current namespace.
use std::collections::*;
Master Rust with Deep Grasping Methodology!Learn More