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

Tommi rusty.gates at icloud.com
Thu Jun 12 11:15:07 PDT 2014


On 2014-06-12, at 20:59, Corey Richardson <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`

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140612/8800d158/attachment.html>


More information about the Rust-dev mailing list