5

I'm able to parse my input into effectively an Iterator<Iterator<i32>> where each inner iterator is length 2. The input looks like:

0: 3
1: 2
2: 4
4: 8
6: 5
8: 6
...

I can parse this with:

input.lines()
     .map(|line| line.split(": ")
                     .filter_map(|n| n.parse::<i32>().ok()))

The best way I came up with to put this into a HashMap is:

let mut tmp_map: HashMap<i32, i32> = HashMap::new();
for mut pair in input.lines()
                     .map(|line| line.split(": ")
                                     .filter_map(|n| n.parse::<i32>().ok()))
{
    tmp_map.insert(pair.next().unwrap(), pair.next().unwrap());
}

...which seems very unwieldy. Is there a way to collect this iterator into a HashMap?

Playground

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
turbulencetoo
  • 3,447
  • 1
  • 27
  • 50

1 Answers1

6

HashMap implements FromIterator<(K, V)>. Then it's just a matter of converting the text into an iterator of tuples. I like using Itertools::tuples:

const INPUT: &str = r#"0: 3
1: 2
2: 4
4: 8
6: 5
8: 6"#;

extern crate itertools;

use std::collections::HashMap;
use itertools::Itertools;

fn main() {
    let z: HashMap<u8, u8> = INPUT
        .lines()
        .flat_map(|l| l.split(":"))
        .flat_map(|n| n.trim().parse())
        .tuples()
        .collect();

    println!("{:?}", z);
}

See also:

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks, `Itertools::tuples` is cool. I had tried `...().collect::<(i32, i32)>` to get those tuples but was informed that it was basically nonsense – turbulencetoo Dec 13 '17 at 22:01
  • 2
    Just as a note: I think this makes a flat stream of numbers, and then picks them in pairs. I.e. it doesn't care whether the input is `1: 2\n3: 4\n`, `1\n2\n3\n4\n` or `1: 2: 3: 4` (also it ignores all parse errors afaict). – Stefan Dec 14 '17 at 07:33
  • @Stefan yep! The original code ignored the parse errors as well (`filter_map(|n| n.parse::().ok())`), I just wrote it shorter. It also makes the assumption that the key and the value are the same integer type. – Shepmaster Dec 14 '17 at 15:14