Reputation: 96586
The .lines()
iterator in Rust yields Result<&str>
s, so you need to do something like this:
let file = File::open("foo")?;
let reader = BufReader::new(file);
let output: Vec<...> = reader
.lines()
.map(|line| line.unwrap())
.map(|line| process_string(line))
.collect();
Except of course, .unwrap()
sucks, so I solved this with process_results
:
let output: Vec<...> = reader
.lines()
.process_results(|ok_lines| ok_lines.map(|line| process_string(line)).collect())?;
This works perfectly. But now I want to do the processing in parallel with Rayon.
let output: Vec<...> = reader
.lines()
.par_bridge()
.map(|line| line.unwrap())
.map(|line| process_string(line))
.collect();
Again this works great, but now... how do I process it in parallel without unwrap()
?
I basically want par_process_results()
but it doesn't seem to exist. Rayon's ParallelIterator
has a few try_
methods like try_fold()
but there doesn't seem to be a try_map()
or try_collect()
.
Upvotes: 0
Views: 47
Reputation: 96586
Oh I forgot about the .collect
trick. This works:
let output: Vec<...> = reader
.lines()
.par_bridge()
.map(|line| Ok(process_string(line?)))
.collect::<Result<Vec<_>, _>>()?;
Upvotes: 0