if I have a Vec<Val>
that I can consume, can I move the elements into a &mut [Val]
?
like you have dst: &mut [Val]
and src: Vec<Val>
?
if so, then {copy,clone}_from_slice
should do the trick
@Dan Gohman for (slot, val) in slots.iter_mut().zip(values) { *slot = val; }
I think?
oh, do what fitzgen says
ah, I guess I was hoping to avoid copy/clone since the old copy is going away anyway, but I can do that
if you want to avoid the clone and Val
's type has a cheap default (e.g. it's a nested Vec
or something) you might also be able to use mem::replace
I tried Alex's thing, changing it to values.into_iter(), but it doesn't like that: cannot move out of
*result which is behind a shared reference
in the worst case you could always do some hackery with unsafe
, a memcpy, and mem::forget
@Dan Gohman can you share the snippet with a tiny bit more context?
though the actual Rust people here will please toss me overboard if that has caveats (Pin
would break I think?)
ok, heh, tried to make a reduced case of this and it compiled without error. let me see what's different...
are you destructuring out of a type that has a Drop
implementation, by chance?
cannot move out of *command_result
which is behind a shared referenc
oops
It looks like I can do it if I have a Vec
, but I actually have a Box<[T]>
@Dan Gohman Vec::from(ins)
in that you can create a Vec
from a Box<[T]>
w/ a zero-cost cast
oh, cool
That works!
I think what was happening is that Box<[T]>
doesn't implement IntoIter
, and it just derefs to the slice's IntoIter
; would need specialization to special case Box<[T]>
over Box<T>
for choosing an IntoIter
, which I'm not sure std
ever does at the public level (although definitely internally uses specialization)
maybe you can clarify, @Alex Crichton
that sounds right yeah
Last updated: Nov 22 2024 at 16:03 UTC