Right now we have operators like
+= which can be overloaded using the
AddAssign trait. Assignment cannot be overloaded, and I agree strongly with that as it would conflict with moving in statements like
a = b.
However, sometimes it is useful to be able to overload assignment of values. As an example, if
reusable_string is a mutable
String, you can append with a simple operator using
reusable_string += string_slice;, but you cannot similarly set the value with a simple operator. To do that you would need to write
reusable_string.clear(); reusable_string += string_slice;, but that is not as ergonomic.
Would it make sense to introduce another operator, for example
:=, for exactly this kind of thing? It would be overloadable with a trait such as
ValAssign which means assign just the value, not the whole structure, and it kind of looks like the other
x= operators with
XAssign traits. Then
reusable_string := string_slice; would leave
reusable_string with the same value as
reusable_string.clear(); reusable_string += string_slice; but it avoids unnecessary reallocation.
String (which I used as an example), this could be useful for mathematics constructs such as large matrices and bignums. For example in the bignum crate Rug, I have a trait
Assign which serves exactly that purpose, but of course it does not have a nice operator to go with it.
(As an extension, the operator could then be used in
let statements to create an object from another object. Keeping with
String for examples, to me
let a: String := string_slice; is nicer than
let a: String = string_slice.into();. It makes sense to me because there
a is taking just the value of
string_slice, so it kind of aligns with “value assignment.”)