[rust-dev] Fwd: &self/&mut self in traits considered harmful(?)

J. Cliff Dyer jcd at sdf.org
Tue Jun 17 01:29:51 PDT 2014

On Tue, 17 Jun 2014 01:03:15 +0200
Sebastian Gesemann <s.gesemann at gmail.com> wrote:

> I'm leaning towards keeping things as they are and telling people who
> want to benefit from move semantics to provide additional functions
> like
>   x.inplace_add(y)
> with
>   fn inplace_add(self, rhs: &SomeBigType) -> SomeBigType {...}
> or something like this.

This puts me in mind of the way python handles operator overloading.
In python, there are several special methods that can be used to
define addition:

An object can have any or all of the following methods:

* foo.__sub__(bar) -- defines the semantics for foo - bar
* foo.__rsub__(bar), (for right-subtract) -- defines how to
  implement bar - foo if bar.__add__(foo) is not defined.
* foo.__isub__(bar) -- defines  foo -= bar.  If __isub__ is not
  defined, it uses foo.__sub__(bar) instead, and re-assigns the
  resulting object to foo.

Would it be possible to define multiple traits that can be used to
implement different desired semantics for the arguments, with a well
defined order of resolution? So there would be a Sub<T, T> and a
RefSub<&T, T> for instance, and RefSub gets used if it exists for the
relevant types, otherwise it falls back to using Sub. That way, Sub
could be kept simple, but by-reference semantics could be supported as


More information about the Rust-dev mailing list