【问题标题】:Convert nested for loops into iterators将嵌套的 for 循环转换为迭代器
【发布时间】:2019-11-10 06:44:26
【问题描述】:

Rust 新手,我无法将以下嵌套的 for 循环转换为迭代器:

#![allow(unused)]

use std::io::Error;

fn main() {
    let realms = vec!["realm".to_string()];
    let auctions = get_auctions(&realms);
}

pub struct Auction;
pub struct RealmAuctionFile;

fn get_realm_auctions(file: &RealmAuctionFile) -> Result<Vec<Auction>, Error> {
    let auctions = vec![Auction {}];

    Ok(auctions)
}

fn get_realm_auction_files(realm: &str) -> Result<Vec<RealmAuctionFile>, Error> {
    let files = vec![RealmAuctionFile {}];

    Ok(files)
}

pub fn get_auctions(realms: &Vec<String>) -> Result<Vec<Auction>, Error> {
    let mut auctions = vec![];

    for realm in realms.iter() {
        let files = get_realm_auction_files(realm)?;

        for file in files.iter() {
            let mut realm_auctions = get_realm_auctions(file)?;

            auctions.append(&mut realm_auctions);
        }
    }

    Ok(auctions)
}

pub fn get_auctions_with_iterator(realms: &Vec<String>) -> Result<Vec<Auction>, Error> {
    let auctions = realms
        .iter()
        .flat_map(|realm| {
            let realms_auctions: Vec<Auction> = get_realm_auction_files(realm)?
                .iter()
                .flat_map(|file| {
                    let auctions = get_realm_auctions(file)?;

                    Ok(auctions)
                })
                .collect();

            Ok(realms_auctions)
        })
        .collect();

    Ok(auctions)
}

Playground

我收到两个错误:

error[E0277]: a collection of type `std::vec::Vec<Auction>` cannot be built from an iterator over elements of type `std::vec::Vec<Auction>`
  --> src/main.rs:52:18
   |
52 |                 .collect();
   |                  ^^^^^^^ a collection of type `std::vec::Vec<Auction>` cannot be built from `std::iter::Iterator<Item=std::vec::Vec<Auction>>`
   |
   = help: the trait `std::iter::FromIterator<std::vec::Vec<Auction>>` is not implemented for `std::vec::Vec<Auction>`

error[E0277]: a collection of type `std::vec::Vec<Auction>` cannot be built from an iterator over elements of type `std::vec::Vec<Auction>`
  --> src/main.rs:56:10
   |
56 |         .collect();
   |          ^^^^^^^ a collection of type `std::vec::Vec<Auction>` cannot be built from `std::iter::Iterator<Item=std::vec::Vec<Auction>>`
   |
   = help: the trait `std::iter::FromIterator<std::vec::Vec<Auction>>` is not implemented for `std::vec::Vec<Auction>`

另外,我无法将.unwrap()“转换”为更惯用的?

【问题讨论】:

  • 与您的主要问题相切,但您不需要在您的flat_map 中使用collect - 您可以返回实现IntoParallelIterator 的任何内容,其中包括anything that implements ParallelIterator。通过收集到Vec,您会进行不必要的分配,并且可能会减慢您的代码速度。 Iterator::flat_map 也是如此。

标签: for-loop rust iterator


【解决方案1】:

不要打开包装。而是生成Results 的迭代器,并利用Result implements FromIterator 将其收集到Result&lt;Vec&gt; 中。

另见this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2012-11-06
    • 2016-03-28
    • 2017-08-08
    • 2021-09-16
    • 2016-02-16
    相关资源
    最近更新 更多