TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
& 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.
- Immutability: Data accessed through a
&Treference 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
&Treferences 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
&Tis 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 aslet, 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.
& 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).
- 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 thestd::ops::BitAndtrait. Any type implementingBitAnd<Rhs>defines the exact behavior and the resultingOutputtype of the&operator.
Master Rust with Deep Grasping Methodology!Learn More





