Re:Rust: Not So Great For Codec Implementing
by SilasX on Tuesday August 01, 2017 @02:21PM
(#14903724)
I meant primitives that preserve Rust's (safe) ownership model.
Re:Rust: Not So Great For Codec Implementing
by geofft on Tuesday August 01, 2017 @02:54PM
(#14904083)
Well, you can use the existing split_at_mut if you're worried about getting your hand-rolled unsafe implementation wrong. It's definitely reasonable if you're splitting on rows; it may or may not be what you want if you're trying to get a slice-like thing that splits on columns.
(There's no reason to avoid safe functions that happen to have unsafe implementations. For instance, the standard-library implementation of indexing a slice, as in b"hello"[3], is just a bounds check plus an unsafe dereference.)
Here's a doc on how to write such a function yourself, and includes the (actual) source code for split_at_mut:
https://doc.rust-lang.org/nomicon/borrow-splitting.html
As others have mentioned, there's nothing special about the standard library in this regard; you can just write the method yourself and it'll work equally well. (The standard library is moderately special in that it gets to use nightly features because dealing with breaking changes is easy for code that literally lives inside Rust's own source repository, but it's not special in terms of its ability to use unsafe code or anything.)