[rust-dev] A better type system

Tommi rusty.gates at icloud.com
Mon Jun 2 00:44:26 PDT 2014


In my original post I stated that it feels like there's something wrong with the language when it doesn't allow multiple mutable references to the same data, but I didn't really explain why it feels like that. So, I just want to add this simple example to help explain my position. It is just plain obvious to everybody that the following code snippet is memory-safe, but the compiler refuses to compile it due to "cannot borrow `stuff[..]` as mutable more than once at a time":

let mut stuff = [1, 2, 3];
let r1 = stuff.mut_slice_to(2);
let r2 = stuff.mut_slice_from(1);

for i in std::iter::range(0u, 2) {
    if i % 2 == 0{
        r1[i] += 1;
    }
    else {
        r2[i] += 2;
    }
}

It's not even possible to forcefully deallocate the memory that is being referenced by multiple reference variables here, and the memory being referenced is just plain old data. Nothing can go wrong here, and yet the compiler thinks something is potentially unsafe and refuses to compile.

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


More information about the Rust-dev mailing list