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.
-= (subtraction assignment) operator is a compound assignment operator that subtracts the right-hand operand from the left-hand operand, mutating the left-hand operand in place with the resulting value.
Syntax and Desugaring
In Rust, -= is syntactic sugar for the sub_assign method. When the compiler encounters this operator, it translates the operation into a method call.
-= are defined by the std::ops::SubAssign trait. To use -= with custom types, you must implement this trait for the left-hand type.
- Mutability Requirement: The left-hand operand must be explicitly bound with
mut. The trait signature requires an exclusive, mutable reference (&mut self) to modify the memory location in place. - Return Type: Unlike C or C++, where assignment operators evaluate to the assigned value, the
-=expression in Rust evaluates to the unit type(). This strictly prevents assignment chaining. For example,let a: i32 = b -= c;will result in a type error, whilelet a = b -= c;compiles successfully but infers the type ofaas()rather than a numerical value. - Ownership and Borrowing: The right-hand operand (
Rhs) is passed by value. For non-Copytypes, this consumes the right-hand operand. However,SubAssigncan be implemented whereRhsis a reference (e.g.,impl SubAssign<&T> for T), allowing the right-hand operand to be borrowed rather than consumed. - Overflow Behavior: When applied to primitive integer types,
-=is subject to Rust’s standard integer overflow rules. An underflow will cause a panic indebugprofiles and perform two’s complement wrapping inreleaseprofiles.
Master Rust with Deep Grasping Methodology!Learn More





