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 is the bitwise OR assignment operator. It performs a bitwise OR operation between a mutable left-hand operand and a right-hand operand, assigning the resulting value directly back to the left-hand operand.
Mechanically, a |= b serves as syntactic sugar for a = a | b, with the strict requirement that the left-hand variable must be declared as mutable (mut).
Underlying Trait Implementation
In Rust’s type system, the|= operator is powered by the std::ops::BitOrAssign trait. When the compiler encounters a |= b, it translates the expression into a method call: a.bitor_assign(b).
|= with custom data structures, you must implement this trait for your type. The method takes an exclusive, mutable reference to self (&mut self) and consumes the rhs operand.
Type Behavior
The operator exhibits specific behaviors depending on the primitive types involved:- Integers (
u8,i32,usize, etc.): Performs a standard bit-level OR operation. Each bit in the result is1if the corresponding bit in either the LHS or RHS is1. Both operands must be of the exact same integer type; Rust does not implicitly coerce types during bitwise assignment. - Booleans (
bool): Performs a strict logical OR operation.
Evaluation Semantics
A critical mechanical distinction of|= when applied to booleans is the absence of short-circuit evaluation. Unlike the standard logical OR operator (||), which halts evaluation if the LHS is true, the |= operator strictly evaluates both the LHS and RHS expressions before executing the assignment.
Master Rust with Deep Grasping Methodology!Learn More





