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

Tommi rusty.gates at icloud.com
Thu Jun 12 10:26:07 PDT 2014


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
}



More information about the Rust-dev mailing list