It’s not forbidden to have two mutable references to the same target if one is a so-called “reborrow” of the other. This can be done in safe rust
fn main() {
let mut i1 = 1;
let pointer = &mut i1;
let pointer2: &mut i32;
pointer2 = &mut *pointer;
println!("Set *pointer2 = 2");
*pointer2 = 2;
/* no good */ // println!("==> Value of {:<8} is {}","pointer", pointer); // Outputs 2
println!("==> Value of {:<8} is {}","pointer2", pointer2); // Outputs 2
/* but it’s okay here */ println!("==> Value of {:<8} is {}","pointer", pointer); // Outputs 2
println!("Set *pointer = 5");
*pointer = 5;
println!("==> Value of {:<8} is {}", "pointer", pointer); // Outputs 5
/* no good */ // println!("==> Value of {:<8} is {}", "pointer2", pointer2); // Outputs 5
}
the point to notice here is that pointer is not usable while pointer2 ist still alive. This is why the first commented-out line won’t work, because pointer2 is used in the following line, so it must be still alive, and also why the final commented-out line won’t work. If you try adding either of the lines in you’ll get an error from the borrow checker. Now, a mutable reference created indirectly through a pointer that you got from another mutable reference, i.e. what your original code does, is totally fine. It’s going to behave like a re-borrow. However, the intermediate step of having a pointer takes the borrow checker out of the picture. While this code
fn main() {
let mut i1 = 1;
let pointer = &mut i1;
let pointer2: &mut i32;
unsafe {
pointer2 = &mut *(pointer as *mut i32);
}
println!("Set *pointer2 = 2");
*pointer2 = 2;
/* no good */ // println!("==> Value of {:<8} is {}","pointer", pointer); // Outputs 2
println!("==> Value of {:<8} is {}","pointer2", pointer2); // Outputs 2
/* but it’s okay here */ println!("==> Value of {:<8} is {}","pointer", pointer); // Outputs 2
println!("Set *pointer = 5");
*pointer = 5;
println!("==> Value of {:<8} is {}", "pointer", pointer); // Outputs 5
/* no good */ // println!("==> Value of {:<8} is {}", "pointer2", pointer2); // Outputs 5
}
is totally fine, uncommenting one of the two commented-out lines will not result in a compiler error, yet it’s still something you’re disallowed to do. [“Disallowed” means it’s UB (“undefined behavior”) i.e. the compiler can do any kinds of nonsensical “optimizations” to your code, as explained above in the thread.] You can test the above code with miri and see how it only complains once you uncomment at least one of the two commented out lines.
A way of phrasing the rules for raw pointers here is: If you use a mutable reference again (in this case the reference pointer) that was used to create a raw pointer (the pointer as *mut i32), then that pointer is invalidated. Once that raw pointer is invalidated, you must no longer use any mutable reference that was created from dereferencing it anymore either (i.e. pointer2 must not be used anymore after pointer is first accessed again after the creation of the pointer as *mut i32 raw pointer).
As you can see, rules for raw pointers (*mut T) in Rust are really complicated and not even 100% finalized yet, so using unsafe in Rust is kind-of hard and in general, especially for some high-level application programming it’s a good idea to not use it if you don’t have to (and you “have” to use it way less commonly than you might think). Also, if you’re taking the source-code of the standard library as an example for unsafe Rust code, the standard library sometimes does things that you must not do as a user. It’s not the case for this example of split_at_mut, but it’s a useful thing to know. The standard library has some extra power in the form of: It’s written by the authors of the compiler and distributed and versioned together with the compiler, so they can rely on implementation details that are subject to change in the future and write code that’s “officially” UB but the compiler doesn’t do any “bad optimizations” to it yet. (In those cases, if user code was to use the same kind of code, it could break and introduce miscompilation in future compiler versions; the code in the standard library would be replaced / adapted together with those kinds of compiler updated.)