[rust-dev] A better type system

Sebastian Gesemann s.gesemann at gmail.com
Tue Jun 3 02:58:08 PDT 2014

On Mon, Jun 2, 2014 at 10:09 PM, Matthew McPherrin wrote:
> On Mon, Jun 2, 2014 at 8:25 AM, Patrick Walton wrote:
>> On 6/2/14 12:44 AM, Tommi wrote:
>>> 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);
>> I'd like to have a function that splits up a vector in that way. That
>> should be doable in the standard library using some unsafe code under the
>> hood.
> Isn't this MutableVector's mut_split_at that we already have?

I thought about mentioning mut_split_at just to make people aware of
it. But the resulting slices are not overlapping which is apparently
what Tommi was interested. My understanding is that even if one uses
an unsafe block to get two overlapping mutable slices, the use of
those might invoke undefined behaviour because it violates some
aliasing assumptions the compiler tends to exploit during
optimizations. Correct me if I'm wrong.


More information about the Rust-dev mailing list