admin管理员组文章数量:1362779
I have src: Vec<Foo>
and dst: &mut [Foo]
. I want to move as many elements as possible from src
into dst
. Specifically:
- If
src
is shorter thandst
, then the beginning ofdst
should be replaced with what was insrc
(leaving the rest ofdst
as-is), andsrc
should end up empty. - If
src
anddst
are the same length, all of the values indst
should be replaced with what was insrc
, andsrc
should end up empty. - If
src
is longer thandst
, all of the values indst
should be replaced with what was in the beginning ofsrc
, andsrc
should be left with only the elements that didn't fit.
For example, pretending Foo
was i32
(but it should work for non-Copy
types too):
- If
src
starts as[1,2]
anddst
starts as[7,8,9,10]
,src
should end up as[]
anddst
should end up as[1,2,9,10]
. - If
src
starts as[1,2,3,4]
anddst
starts as[7,8,9,10]
,src
should end up as[]
anddst
should end up as[1,2,3,4]
. - If
src
starts as[1,2,3,4,5,6]
anddst
starts as[7,8,9,10]
,src
should end up as[5,6]
anddst
should end up as[1,2,3,4]
.
I know I could loop manually and move one element at a time, but it seems like there should be a Drain
-like approach instead.
I have src: Vec<Foo>
and dst: &mut [Foo]
. I want to move as many elements as possible from src
into dst
. Specifically:
- If
src
is shorter thandst
, then the beginning ofdst
should be replaced with what was insrc
(leaving the rest ofdst
as-is), andsrc
should end up empty. - If
src
anddst
are the same length, all of the values indst
should be replaced with what was insrc
, andsrc
should end up empty. - If
src
is longer thandst
, all of the values indst
should be replaced with what was in the beginning ofsrc
, andsrc
should be left with only the elements that didn't fit.
For example, pretending Foo
was i32
(but it should work for non-Copy
types too):
- If
src
starts as[1,2]
anddst
starts as[7,8,9,10]
,src
should end up as[]
anddst
should end up as[1,2,9,10]
. - If
src
starts as[1,2,3,4]
anddst
starts as[7,8,9,10]
,src
should end up as[]
anddst
should end up as[1,2,3,4]
. - If
src
starts as[1,2,3,4,5,6]
anddst
starts as[7,8,9,10]
,src
should end up as[5,6]
anddst
should end up as[1,2,3,4]
.
I know I could loop manually and move one element at a time, but it seems like there should be a Drain
-like approach instead.
1 Answer
Reset to default 4This is easy to do just by figuring out how many elements you can move (which is the minimum of both lengths) and then using drain
to remove those elements and place them in the destination.
pub fn move_many<T>(src: &mut Vec<T>, dst: &mut [T]) {
let count = src.len().min(dst.len());
for (s, d) in src.drain(0..count).zip(dst) {
*d = s;
}
}
(Playground)
本文标签: rustHow do I move as many elements as possible from a Vec into a preexisting sliceStack Overflow
版权声明:本文标题:rust - How do I move as many elements as possible from a Vec into a pre-existing slice? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743821806a2544920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Vec::drain
? It seems unlikely something more specific is available. – cafce25 Commented 2 days agozip
, so what I thought I'd have to do was more complicated than necessary. I'm going to give cdhowie's answer a try now, since it looks like exactly what I need. – Joseph Sible-Reinstate Monica Commented 2 days agosrc
. – Chayim Friedman Commented 2 days ago