Trait std::iter::FromIterator

1.0.0 · source ·
pub trait FromIterator<A>: Sized {
    // Required method
    fn from_iter<T>(iter: T) -> Self
       where T: IntoIterator<Item = A>;
}
Expand description

Iterator 转换。

通过为类型实现 FromIterator,可以定义如何从迭代器创建它。 这对于描述某种集合的类型很常见。

如果想从迭代器的内容中创建一个集合,则首选 Iterator::collect() 方法。 但是,当您需要指定容器类型时,FromIterator::from_iter() 比使用 turbofish 更具可读性 (例如

::<Vec<_>>()). 有关其使用的更多示例,请参见 Iterator::collect() 文档。

另请参见:IntoIterator

Examples

基本用法:

let five_fives = std::iter::repeat(5).take(5);

let v = Vec::from_iter(five_fives);

assert_eq!(v, vec![5, 5, 5, 5, 5]);
Run

使用 Iterator::collect() 隐式使用 FromIterator

let five_fives = std::iter::repeat(5).take(5);

let v: Vec<i32> = five_fives.collect();

assert_eq!(v, vec![5, 5, 5, 5, 5]);
Run

使用 FromIterator::from_iter() 作为更易读的替代方案 Iterator::collect():

use std::collections::VecDeque;
let first = (0..10).collect::<VecDeque<i32>>();
let second = VecDeque::from_iter(0..10);

assert_eq!(first, second);
Run

为您的类型实现 FromIterator

// 一个样本集合,这只是 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);
    }
}

// 我们将实现 FromIterator
impl FromIterator<i32> for MyCollection {
    fn from_iter<I: IntoIterator<Item=i32>>(iter: I) -> Self {
        let mut c = MyCollection::new();

        for i in iter {
            c.add(i);
        }

        c
    }
}

// 现在我们可以创建一个新的迭代器...
let iter = (0..5).into_iter();

// ... 并用它制作一个 MyCollection
let c = MyCollection::from_iter(iter);

assert_eq!(c.0, vec![0, 1, 2, 3, 4]);

// 也收集作品!

let iter = (0..5).into_iter();
let c: MyCollection = iter.collect();

assert_eq!(c.0, vec![0, 1, 2, 3, 4]);
Run

Required Methods§

source

fn from_iter<T>(iter: T) -> Selfwhere T: IntoIterator<Item = A>,

从迭代器创建一个值。

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

Examples

基本用法:

let five_fives = std::iter::repeat(5).take(5);

let v = Vec::from_iter(five_fives);

assert_eq!(v, vec![5, 5, 5, 5, 5]);
Run

Implementors§

source§

impl FromIterator<char> for String

1.23.0 · source§

impl FromIterator<()> for ()

将一个迭代器中的所有 unit 项折叠为一个。

与更高级别的抽象结合使用时,此功能尤其有用,例如收集到仅关心错误的 Result<(), E> 上:

use std::io::*;
let data = vec![1, 2, 3, 4, 5];
let res: Result<()> = data.iter()
    .map(|x| writeln!(stdout(), "{x}"))
    .collect();
assert!(res.is_ok());
Run
1.45.0 · source§

impl FromIterator<Box<str, Global>> for String

1.52.0 · source§

impl FromIterator<OsString> for OsString

1.4.0 · source§

impl FromIterator<String> for String

1.17.0 · source§

impl<'a> FromIterator<&'a char> for String

source§

impl<'a> FromIterator<&'a str> for String

1.52.0 · source§

impl<'a> FromIterator<&'a OsStr> for OsString

1.19.0 · source§

impl<'a> FromIterator<Cow<'a, str>> for String

1.52.0 · source§

impl<'a> FromIterator<Cow<'a, OsStr>> for OsString

1.12.0 · source§

impl<'a> FromIterator<char> for Cow<'a, str>

1.12.0 · source§

impl<'a> FromIterator<String> for Cow<'a, str>

1.12.0 · source§

impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str>

source§

impl<'a, T> FromIterator<T> for Cow<'a, [T]>where T: Clone,

source§

impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E>where V: FromIterator<A>,

source§

impl<A, V> FromIterator<Option<A>> for Option<V>where V: FromIterator<A>,

1.32.0 · source§

impl<I> FromIterator<I> for Box<[I], Global>

source§

impl<K, V> FromIterator<(K, V)> for BTreeMap<K, V, Global>where K: Ord,

source§

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>where K: Eq + Hash, S: BuildHasher + Default,

source§

impl<P: AsRef<Path>> FromIterator<P> for PathBuf

source§

impl<T> FromIterator<T> for BTreeSet<T, Global>where T: Ord,

source§

impl<T> FromIterator<T> for BinaryHeap<T>where T: Ord,

source§

impl<T> FromIterator<T> for LinkedList<T, Global>

source§

impl<T> FromIterator<T> for VecDeque<T, Global>

1.37.0 · source§

impl<T> FromIterator<T> for Rc<[T]>

1.37.0 · source§

impl<T> FromIterator<T> for Arc<[T]>

source§

impl<T> FromIterator<T> for Vec<T, Global>

source§

impl<T, S> FromIterator<T> for HashSet<T, S>where T: Eq + Hash, S: BuildHasher + Default,