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 right shift assignment operator. It shifts the binary representation of the left operand to the right by the number of bits specified by the right operand, and assigns the resulting value back to the left operand in place.
Syntax and Desugaring
The expressionx >>= y requires the left operand to be mutable. In Rust, compound assignment operators do not desugar to their expanded assignment forms (i.e., x = x >> y). Instead, x >>= y desugars directly to a mutable borrow and a method call on the std::ops::ShrAssign trait: ShrAssign::shr_assign(&mut x, y).
Type-Dependent Behavior
The behavior of the right shift operation depends strictly on the signedness of the left operand’s type:-
Unsigned Integers (
u8,u16,u32,u64,u128,usize): Performs a logical right shift. The bits are shifted to the right, and the newly vacated most significant bits (MSB) are filled with zeros.
Trait Implementation
The>>= operator is backed by the std::ops::ShrAssign trait.
Rhs) is a generic parameter, the shift amount does not need to be the same type as the value being shifted. Rust’s standard library implements ShrAssign for all combinations of primitive integer types.
Overflow Behavior
If the right operand (the shift amount) is greater than or equal to the number of bits in the left operand’s type, the behavior depends on the compilation profile. In safe Rust, shifting by an amount greater than or equal to the type’s bit-width is never undefined behavior.- Debug mode: The operation panics. This panic exists strictly to catch programmer logic errors (integer overflow).
- Release mode: The shift amount is masked (modulo the bit-width of the type) before the shift is applied. For example, shifting a
u8(which has an 8-bit width) by 9 bits will actually shift by9 % 8 = 1bit.
Master Rust with Deep Grasping Methodology!Learn More





