| ▲ | loeg 10 hours ago |
| > For (2), Rust enforces this invariant via the borrow checker by disallowing (at compile-time) a shared slice reference that points to an overlapping mutable slice reference. At least the last time I cared about this, the borrow checker wouldn't allow mutable and immutable borrows from the same underlying object, even if they did not overlap. (Which is more restrictive, in an obnoxious way.) |
|
| ▲ | Cyph0n 9 hours ago | parent [-] |
| Do you mean borrows for different fields of a struct? If so, that’s handled today - it’s sometimes called “splitting borrows”: https://doc.rust-lang.org/nomicon/borrow-splitting.html |
| |
| ▲ | loeg 9 hours ago | parent [-] | | Not exactly -- independent subranges of the same range (as would be relevant to something like memcpy/memmove/strcpy). E.g., https://godbolt.org/z/YhGajnhEG It's mentioned later in the same article you shared above. | | |
| ▲ | oneshtein 10 minutes ago | parent | next [-] | | fn f() {
let mut v = vec![1, 2, 3, 4, 5];
let (header, tail) = v.split_at_mut(1);
b(&header[0], &mut tail[0]);
}
| |
| ▲ | Cyph0n 9 hours ago | parent | prev [-] | | Gotcha. There is a split_at_mut method that splits a mutable slice reference into two. That doesn’t address the problem you had, but I think that’s best you can do with safe Rust. | | |
| ▲ | loeg 8 hours ago | parent [-] | | Yeah. It just isn't something the borrow checker natively understands. |
|
|
|