Borrow-checking surprises

73 points by jamii


lyxia

The examples in the post are all used with primitive types (usize, i32), which are the special case where x += y evaluates differently from x.add_assign(y). Quoting the Rust Reference:

If the types of both operands are known, prior to monomorphization, to be primitive, the right hand side is evaluated first, the left hand side is evaluated next, and the place given by the evaluation of the left hand side is mutated by applying the operator to the values of both sides.

Otherwise, this expression is syntactic sugar for using the corresponding trait for the operator (...)

In other words, += evaluates left-to-right or right-to-left depending on the type of its operands: https://gist.github.com/Lysxia/972d06eb7d73a1bbea71a03f0c1b00d5