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

Sebastian Gesemann s.gesemann at gmail.com
Mon Jun 16 07:47:32 PDT 2014

On Mon, Jun 16, 2014 at 4:32 PM, Sebastian Gesemann
> [...]
> Assuming this RFC is accepted: How would I have to implement Add for a
> custom type T where moving doesn't make much sense and I'd rather use
> immutable references to bind the operands? I could write
>    impl Add<&T,T> for &T {...}
> but it seems to me that this requires explicit borrowing in the user code à la
>    let x: T = ...;
>    let y: T = ...;
>    let c = &x + &y;
> Or is this also handled via implicit borrowing for operators (making
> operators a special case)?
> Still, I find it very weird to impl Add for &T instead of T and have
> this asymmetry between &T and T for operands and return value.

On top of that, it seems that this would make the writing of generics
pretty messy. Suddenly, the following generic function won't work
anymore for my T:

  fn foo<R,T:Add<T,R>>(x: &T, y: &T) -> R {
    *x + *y

because T is not an Add<T,R>, but &T is an Add<&T,R>.


More information about the Rust-dev mailing list