Trait std::iter::IntoIterator

1.0.0 · source ·
pub trait IntoIterator {
    type Item;
    type IntoIter: Iterator<Item = Self::Item>;

    // Required method
    fn into_iter(self) -> Self::IntoIter;
}
Expand description

转换为 Iterator

通过为类型实现 IntoIterator,可以定义如何将其转换为迭代器。 这对于描述某种集合的类型很常见。

实现 IntoIterator 的好处之一是您的类型将为 使用 Rust 的 for 循环语法

另请参见:FromIterator

Examples

基本用法:

let v = [1, 2, 3];
let mut iter = v.into_iter();

assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());
Run

为您的类型实现 IntoIterator

// 一个样本集合,这只是 Vec<T> 的包装
#[derive(Debug)]
struct MyCollection(Vec<i32>);

// 让我们给它一些方法,以便我们可以创建一个方法并向其中添加一些东西。
impl MyCollection {
    fn new() -> MyCollection {
        MyCollection(Vec::new())
    }

    fn add(&mut self, elem: i32) {
        self.0.push(elem);
    }
}

// 我们将实现 IntoIterator
impl IntoIterator for MyCollection {
    type Item = i32;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

// 现在我们可以做一个新的集合...
let mut c = MyCollection::new();

// ... 添加一些东西...
c.add(0);
c.add(1);
c.add(2);

// ... 然后将其转换为迭代器:
for (i, n) in c.into_iter().enumerate() {
    assert_eq!(i as i32, n);
}
Run

通常将 IntoIterator 用作 trait bound。只要它仍然是迭代器,就可以更改输入集合类型。 可以通过限制限制来指定其他范围 Item:

fn collect_as_strings<T>(collection: T) -> Vec<String>
where
    T: IntoIterator,
    T::Item: std::fmt::Debug,
{
    collection
        .into_iter()
        .map(|item| format!("{item:?}"))
        .collect()
}
Run

Required Associated Types§

source

type Item

被迭代的元素的类型。

source

type IntoIter: Iterator<Item = Self::Item>

我们将其变成哪种迭代器?

Required Methods§

source

fn into_iter(self) -> Self::IntoIter

从一个值创建一个迭代器。

有关更多信息,请参见 模块级文档

Examples

基本用法:

let v = [1, 2, 3];
let mut iter = v.into_iter();

assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());
Run

Implementors§

1.10.0 · source§

impl<'a> IntoIterator for &'a UnixListener

Available on Unix only.
1.6.0 · source§

impl<'a> IntoIterator for &'a Path

§

type Item = &'a OsStr

§

type IntoIter = Iter<'a>

1.6.0 · source§

impl<'a> IntoIterator for &'a PathBuf

§

type Item = &'a OsStr

§

type IntoIter = Iter<'a>

source§

impl<'a, K, V, A> IntoIterator for &'a BTreeMap<K, V, A>where A: Allocator + Clone,

§

type Item = (&'a K, &'a V)

§

type IntoIter = Iter<'a, K, V>

source§

impl<'a, K, V, A> IntoIterator for &'a mut BTreeMap<K, V, A>where A: Allocator + Clone,

§

type Item = (&'a K, &'a mut V)

§

type IntoIter = IterMut<'a, K, V>

source§

impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>

§

type Item = (&'a K, &'a V)

§

type IntoIter = Iter<'a, K, V>

source§

impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>

§

type Item = (&'a K, &'a mut V)

§

type IntoIter = IterMut<'a, K, V>

1.4.0 · source§

impl<'a, T> IntoIterator for &'a Option<T>

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T> IntoIterator for &'a [T]

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T> IntoIterator for &'a BinaryHeap<T>

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

1.1.0 · source§

impl<'a, T> IntoIterator for &'a Receiver<T>

§

type Item = T

§

type IntoIter = Iter<'a, T>

1.4.0 · source§

impl<'a, T> IntoIterator for &'a mut Option<T>

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

source§

impl<'a, T> IntoIterator for &'a mut [T]

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a BTreeSet<T, A>where A: Allocator + Clone,

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a LinkedList<T, A>where A: Allocator,

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a VecDeque<T, A>where A: Allocator,

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a Vec<T, A>where A: Allocator,

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a mut LinkedList<T, A>where A: Allocator,

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a mut VecDeque<T, A>where A: Allocator,

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

source§

impl<'a, T, A> IntoIterator for &'a mut Vec<T, A>where A: Allocator,

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

1.4.0 · source§

impl<'a, T, E> IntoIterator for &'a Result<T, E>

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

1.4.0 · source§

impl<'a, T, E> IntoIterator for &'a mut Result<T, E>

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

source§

impl<'a, T, S> IntoIterator for &'a HashSet<T, S>

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, const N: usize> IntoIterator for &'a [T; N]

§

type Item = &'a T

§

type IntoIter = Iter<'a, T>

source§

impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]

§

type Item = &'a mut T

§

type IntoIter = IterMut<'a, T>

const: unstable · source§

impl<I> IntoIterator for Iwhere I: Iterator,

§

type Item = <I as Iterator>::Item

§

type IntoIter = I

source§

impl<K, V, A> IntoIterator for BTreeMap<K, V, A>where A: Allocator + Clone,

§

type Item = (K, V)

§

type IntoIter = IntoIter<K, V, A>

source§

impl<K, V, S> IntoIterator for HashMap<K, V, S>

§

type Item = (K, V)

§

type IntoIter = IntoIter<K, V>

source§

impl<T> IntoIterator for Option<T>

§

type Item = T

§

type IntoIter = IntoIter<T>

source§

impl<T> IntoIterator for BinaryHeap<T>

§

type Item = T

§

type IntoIter = IntoIter<T>

1.1.0 · source§

impl<T> IntoIterator for Receiver<T>

§

type Item = T

§

type IntoIter = IntoIter<T>

source§

impl<T, A> IntoIterator for BTreeSet<T, A>where A: Allocator + Clone,

§

type Item = T

§

type IntoIter = IntoIter<T, A>

source§

impl<T, A> IntoIterator for LinkedList<T, A>where A: Allocator,

§

type Item = T

§

type IntoIter = IntoIter<T, A>

source§

impl<T, A> IntoIterator for VecDeque<T, A>where A: Allocator,

§

type Item = T

§

type IntoIter = IntoIter<T, A>

source§

impl<T, A> IntoIterator for Vec<T, A>where A: Allocator,

§

type Item = T

§

type IntoIter = IntoIter<T, A>

source§

impl<T, E> IntoIterator for Result<T, E>

§

type Item = T

§

type IntoIter = IntoIter<T>

source§

impl<T, S> IntoIterator for HashSet<T, S>

§

type Item = T

§

type IntoIter = IntoIter<T>

1.53.0 · source§

impl<T, const N: usize> IntoIterator for [T; N]

§

type Item = T

§

type IntoIter = IntoIter<T, N>