11# Splitting Borrows
22
33The mutual exclusion property of mutable references can be very limiting when
4- working with a composite structure. The borrow checker understands some basic
5- stuff, but will fall over pretty easily. It does understand structs
6- sufficiently to know that it's possible to borrow disjoint fields of a struct
7- simultaneously. So this works today:
4+ working with a composite structure. The borrow checker (a.k.a. borrowck)
5+ understands some basic stuff, but will fall over pretty easily. It does
6+ understand structs sufficiently to know that it's possible to borrow disjoint
7+ fields of a struct simultaneously. So this works today:
88
99``` rust
1010struct Foo {
@@ -23,8 +23,8 @@ let c2 = &x.c;
2323println! (" {} {} {} {}" , a , b , c , c2 );
2424```
2525
26- However borrowck doesn't understand arrays or slices in any way, so this doesn't
27- work:
26+ However the borrow checker doesn't understand arrays or slices in any way, so
27+ this doesn't work:
2828
2929``` rust,compile_fail
3030let mut x = [1, 2, 3];
@@ -48,17 +48,17 @@ error[E0499]: cannot borrow `x[..]` as mutable more than once at a time
4848error: aborting due to previous error
4949```
5050
51- While it was plausible that borrowck could understand this simple case, it's
52- pretty clearly hopeless for borrowck to understand disjointness in general
53- container types like a tree, especially if distinct keys actually * do * map
54- to the same value.
55-
56- In order to "teach" borrowck that what we're doing is ok, we need to drop down
57- to unsafe code. For instance, mutable slices expose a ` split_at_mut ` function
58- that consumes the slice and returns two mutable slices. One for everything to
59- the left of the index, and one for everything to the right. Intuitively we know
60- this is safe because the slices don't overlap, and therefore alias. However
61- the implementation requires some unsafety:
51+ While it was plausible that the borrow checker could understand this simple
52+ case, it's pretty clearly hopeless for the borrow checker to understand
53+ disjointness in general container types like a tree, especially if distinct keys
54+ actually * do * map to the same value.
55+
56+ In order to "teach" the borrow checker that what we're doing is ok, we need to
57+ drop down to unsafe code. For instance, mutable slices expose a ` split_at_mut `
58+ function that consumes the slice and returns two mutable slices. One for
59+ everything to the left of the index, and one for everything to the
60+ right. Intuitively we know this is safe because the slices don't overlap, and
61+ therefore alias. However the implementation requires some unsafety:
6262
6363``` rust
6464# use std :: slice :: from_raw_parts_mut;
0 commit comments