Mutable mutable references

Continuing the discussion from How to "memcpy" bytes in stable rust:

fn byte_copy(from: &[u8], mut to: &mut [u8]) -> usize {
                          ^~~
    to.write(&from).unwrap()
}

Why do we need this mut? In my understanding I'd need it to do this

    to = &mut foo;

But it's not like a method on &mut self could make to point somewhere else.

Write is implemented for &mut [u8] and the method takes &mut self, which is &mut &mut [u8].

It actually updates the slice, so that it only points to the part that wasn't written to.

Edit: Source code

2 Likes