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

Patrick Walton pcwalton at mozilla.com
Thu Jun 12 11:15:42 PDT 2014


On 6/12/14 11:15 AM, Tommi wrote:
> On 2014-06-12, at 20:59, Corey Richardson <corey at octayn.net
> <mailto:corey at octayn.net>> wrote:
>
>> Implicit cloning is a non-starter. Clones can be very expensive.
>> Hiding that cost is undesirable and would require adding Clone to the
>> language (it's currently a normal library feature).
>
> But I think it will be easy to make the error of writing the explicit
> .clone() in places where it's not needed. For example:
>
> fn foo<T>(value: T) {}
>
> let x = box 123;
> x.clone().foo();
> x.clone().foo();
>
> ...given that `x` is not used after those lines, the last call to
> .clone() is unnecessary. Whereas, if the task of cloning (implicitly) is
> assigned to the compiler, then the compiler can use static analysis to
> make sure such programming errors never occur. The example above would
> become something like:
>
> fn foo<T>(stable value: T) {}
>
> let x = box 123;
> x.foo(); // here `x` gets cloned here
> x.foo(); // here `x` doesn't get cloned because this is the last use of `x`

We tried that in earlier versions of Rust. There were way too many clones.

Patrick



More information about the Rust-dev mailing list