[rust-dev] mode removal

Graydon Hoare graydon at mozilla.com
Wed Jul 25 13:11:00 PDT 2012


On 12-07-25 12:57 PM, Gareth Smith wrote:
> On 25/07/12 13:44, Niko Matsakis wrote:
> 
>> Sound good?
> 
> Yes! I think modes are unintuitive and awkward, and that removing them
> is a good thing.

Yes. They exist for only two reasons:

  (1) we used to support type parameters via runtime dispatch, now we
      monomorphize, and are comfortable paying that cost.

  (2) we used to have no first-class borrowed pointers, but now we
      do.

IOW they're entirely redundant now. We've just been postponing their
removal until we're sure borrowed pointers work right. They seem to, or
at least well enough, now.

> To clarify how the migration will work, how will code like this look
> during a. the migration phase and b. after the migration?
> 
> fn shizzle(x: {mut a: int}) {
>     x.a = 2;
> }
> 
> fn main() {
>     let x = {mut a: 1};
>     shizzle(x);
>     io::println(#fmt("x.a = %d", x.a)); // prints "x.a = 2"
> }

During and after migration -- indeed, you can write this today, and
should be doing so in new code -- the borrowed-pointer-using code looks
like so:

fn shizzle(x: &{mut a: int}) {
    x.a = 2;
}

fn main() {
    let x = {mut a: 1}; // x is the record
    shizzle(&x);        // &x is a pointer
    io::println(#fmt("x.a = %d", x.a));
}

Or, alternatively, writing main as so:

fn main() {
    let x = &{mut a: 1};  // x is a pointer
    shizzle(x);
    io::println(#fmt("x.a = %d", x.a));
}

Both should work, just a matter of whether you want x to be a pointer,
or the record itself. The "migration" is just a matter of going through
all the existing code and rewriting it to use such newer idioms.

-Graydon



More information about the Rust-dev mailing list