将迭代器推送到向量(或任何集合)

use*_*018 9 rust

通过Rust的文档,我只看到了以下方法

fn push_all(&mut self, other: &[T])
Run Code Online (Sandbox Code Playgroud)

将多个值推送到已存在的值Vec.但是,如果我有一个迭代器,我认为它使用效率不高:vector.push_all(it.collect().as_ref()).有办法实现吗?

fjh*_*fjh 14

您可以使用Vecextend方法.extend获取实现的任何类型的值IntoIterator(包括所有迭代器类型)并扩展Vec迭代器返回的所有元素.

由于extend属于Extend特征,它也适用于许多其他集合类型.