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

Corey Richardson corey at octayn.net
Thu Jun 12 10:30:26 PDT 2014


It's called Copy. `trait Foo: Copy { ... }`.

On Thu, Jun 12, 2014 at 10:26 AM, Tommi <rusty.gates at icloud.com> wrote:
> I think a new keyword, something like `stable`, is needed for specifying that an argument passed to a trait function is guaranteed to be logically unchanged after the function call. For example:
>
> trait Foo {
>     fn foo(stable self);
> }
>
> impl Foo for int {
>     fn foo(&self) {} // OK
> }
>
> impl Foo for uint {
>     fn foo(self) {} // OK
> }
>
> impl Foo for Box<int> {
>     fn foo(stable self) {} // OK (implicitly clones self)
> }
>
>
> fn main() {
>     let x: Box<int> = box 42;
>     x.foo(); // `x` is implicitly cloned
>     x.foo(); // OK
> }
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



-- 
http://octayn.net/


More information about the Rust-dev mailing list