I can create a consuming iterator in the heap:
vec![1, 10, 100].into_iter()
I can also create an iterator on the stack that borrows the elements:
[1, 10, 100].iter()
But if I write this:
[1, 10, 100].into_iter()
This is not a consuming iterator because [T; _]::into_iter
does not exist: IntoIterator
is only implemented for the borrowed version (aka slice). Is there a simple way (preferably in the std
lib) to create a consuming iterator on the stack?
I know that [1, 10, 100].iter().cloned()
can be done, but this requires the items to be clonable.
Is there a simple way (preferably in the
std
lib) to create a consuming iterator on the stack?
No.
Is there a
simpleway(preferably in the std lib)to create a consuming iterator on the stack?
Yes. Use a crate like stack
or smallvec
, which provide array types that implement IntoIterator
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With