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 & operator in Rust primarily functions as the reference operator (or borrow operator), creating an immutable, shared reference to an existing value without transferring ownership. Secondarily, it serves as the bitwise AND operator for integer and boolean types.

1. The Reference Operator (Shared Borrow)

When applied to an expression as a unary prefix operator, & yields a pointer to the memory address of the underlying data. At the type-system level, applying & to a value of type T produces a value of type &T.
let val: i32 = 10;
let ref_val: &i32 = &val; // Creates an immutable reference to `val`
Semantics and Memory:
  • Immutability: Data accessed through a &T reference cannot be mutated. The memory is strictly read-only through this pointer, unless interior mutability primitives (e.g., RefCell<T>, Mutex<T>) are utilized.
  • Aliasing and Liveness: Rust’s borrow checker enforces that multiple &T references to the same data can exist concurrently, provided no mutable reference (&mut T) is active concurrently. Due to Non-Lexical Lifetimes (NLL), mutable and immutable references can exist in the same lexical scope as long as their active lifetimes (liveness) do not overlap.
  • Lifetimes: Every &T is implicitly or explicitly bound to a lifetime (&'a T). The compiler ensures the reference cannot outlive the memory location it points to, preventing dangling pointers.

2. Pattern Destructuring

In pattern matching (such as let, match, or function arguments), & is used to destructure references. It acts as the inverse of the reference operator, stripping the reference to match against the underlying data.
// Binding by value (requires `Copy`)
let ref_val: &i32 = &42;
let &deref_val = ref_val; // `deref_val` is bound as type `i32`

// Ignoring or binding by reference (does not require `Copy`)
let non_copy_ref: &String = &String::new();
let &_ = non_copy_ref; // Value is ignored
let &ref bound_ref = non_copy_ref; // Bound as `&String`
Semantics: Destructuring a reference with & only requires the underlying type T to implement the Copy trait if the pattern attempts to bind the underlying data by value. If T is not Copy, attempting to bind it by value will cause a compiler error, as it violates memory safety by attempting to move a value out of a borrowed context. However, if the pattern ignores the value (e.g., _) or binds it by reference (e.g., ref), the underlying type does not need to implement Copy.

3. Bitwise and Logical AND

When placed between two operands as a binary infix operator, & evaluates to the bitwise AND (for integers) or eager logical AND (for booleans).
// Bitwise AND (Integers)
let a: u8 = 0b1100;
let b: u8 = 0b1010;
let c: u8 = a & b; // Evaluates to 0b1000

// Eager Logical AND (Booleans)
let bool_res: bool = true & false; // Evaluates to false
Semantics and Trait Resolution:
  • Non-short-circuiting: Unlike the && operator, the binary & operator evaluates both the left and right operands before applying the AND operation.
  • Trait Implementation: The binary behavior of & is governed by the std::ops::BitAnd trait. Any type implementing BitAnd<Rhs> defines the exact behavior and the resulting Output type of the & operator.
Master Rust with Deep Grasping Methodology!Learn More