4 ms·
> Rust doesn’t support this out of the box, but you can get something equivalent with the owning_ref crate Rust does come with something like this out of the b
by _bxg1 6y ago
> Rust doesn’t support this out of the box, but you can get something equivalent with the owning_ref crate
Rust does come with something like this out of the box for the case of arrays: https://doc.rust-lang.org/std/primitive.slice.html#method.split_at_mut https://doc.rust-lang.org/std/primitive.slice.html#method.sp...
- saagarjha 6y agoI wonder if Rust could provide some sort of “disjoint slice” type that would allow for this method to take a predicate for more complex divisions. (Interestingly, this exact use case is one of the few places where C++’s stringent iterator invalidation requirements come in handy, because they let you pass unordered_map buckets safely to different threads.)
- comex 6y agosplit_at_mut is for borrowed references. That's the case where I said it does work out of the box with fields, since you can take mutable references to disjoint fields, and the borrow checker allows it without you having to do anything special. But if you want to take an owned pointer to an object and split it into owned pointers to its subcomponents (which only makes sense with Rc), I don't think the standard library has a way to do that for either fields or arrays.
- _bxg1 6y agoAh, that makes sense my mistake
- mrathi12 6y agoThis is all unsafe code under the hood though - the ownership type system doesn't technically allow it but to increase expressivity they do provide this split() method for programmers to use