gpt4 book ai didi

collections - 如何将迭代器推送到现有向量(或任何其他集合)?

转载 作者:行者123 更新时间:2023-11-29 07:44:23 25 4
gpt4 key购买 nike

查看文档或 Rust 0.12,我看到了 the following method将多个值推送到一个已经存在的 Vec:

fn push_all(&mut self, other: &[T])

但是,如果我有迭代器,我认为使用它效率不高:vector.push_all(it.collect().as_ref())。有没有更有效的方法?

最佳答案

您可以使用 Vecextend method . extend 接受实现了 IntoIterator(包括所有迭代器类型)的任何类型的值,并通过从迭代器返回的所有元素扩展 Vec:

vector.extend(it)

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

关于collections - 如何将迭代器推送到现有向量(或任何其他集合)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com