gpt4 book ai didi

iterator - 如何在堆栈上创建按值迭代器?

转载 作者:行者123 更新时间:2023-11-29 07:52:10 32 4
gpt4 key购买 nike

我可以在堆中创建一个消费迭代器:

vec![1, 10, 100].into_iter()

我还可以在堆栈上创建一个迭代器来借用元素:

[1, 10, 100].iter()

但是如果我这样写:

[1, 10, 100].into_iter()

这不是一个消费迭代器,因为 [T; _]::into_iter 不存在:IntoIterator 仅针对借用版本(又名切片)实现。是否有一种简单的方法(最好在 std 库中)在堆栈上创建一个消费迭代器?


我知道 [1, 10, 100].iter().cloned() 可以完成,但这需要项目是可克隆的。

最佳答案

Is there a simple way (preferably in the std lib) to create a consuming iterator on the stack?

没有。

Is there a simple way (preferably in the std lib) to create a consuming iterator on the stack?

是的。使用像 stack 这样的 crate 或 smallvec ,它提供实现 IntoIterator 的数组类型。

关于iterator - 如何在堆栈上创建按值迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52605781/

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