1

Rust-newbie here, I'm having trouble converting the following nested for loops into iterators:

#![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

I get two errors:

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>`

Also, I can't "convert" the .unwrap() with the more idiomatic ?

Stargateur
  • 24,473
  • 8
  • 65
  • 91
shempignon
  • 562
  • 3
  • 10
  • Tangential to your main question, but you should not need to `collect` inside your `flat_map` - you're allowed to return anything that implements [`IntoParallelIterator`](https://docs.rs/rayon/1.1.0/rayon/iter/trait.IntoParallelIterator.html), which includes [anything that implements `ParallelIterator`](https://docs.rs/rayon/1.1.0/rayon/iter/trait.IntoParallelIterator.html#implementors). By collecting into a `Vec`, you're needlessly allocating, and potentially slowing down your code. The same goes for `Iterator::flat_map` too. – Joe Clay Jun 28 '19 at 12:15

1 Answers1

2

Don't unwrap. Instead generate and iterator of Results and take advantage of the fact that Result implements FromIterator to collect it into a Result<Vec>.

See also this answer.

Jmb
  • 18,893
  • 2
  • 28
  • 55