My problem
I have a boxed slice s: Box<[Something]>
that I would like to split into two owned halves, e.g., s1: Box<[Something]>
containing the first s.len()/2
elements of s
, and s2: Box<[Something]>
containing the rest.
With Vec
s
I know something along these lines can be achieved if s: Vec<Something>
, using s.split_off()
. However, what split_off
does is it creates a new t: Vec<Something>
, moves s.len()/2
elements from s
to t
, then shrinks s
. While this obviously works, it does not feel very efficient.
Unsafely speaking
If I were writing in C, what I would is just take a pointer to the middle of s
, call it t
and unsafely ride into the sunset. I am wondering if something like this can be done in Rust too.
(One issue that I see with the above C-style approach is that, while s
can be freed, t
cannot. So s
and t
would not be exactly the same, as one of the two would need to be freed, while the other should just be dropped. But, I could imagine this information being carried in some flag attached to s
and t
.)