I turn a regex into a HashSet
after doing some filtering. I am trying to use it with Rayon, but I can't figure out how to make Rayon work with an existing iterator without converting it to a vector first. Is this possible?
let re = Regex::new("url=\"(?P<url>.+?)\"").unwrap();
let urls: HashSet<String> = re.captures_iter(&contents)
.map(|m| Url::parse(m.name("url").unwrap().as_str()))
.filter(|parsed_url| parsed_url.is_ok())
.map(|parsed_url| parsed_url.unwrap())
.filter(|parsed_url| parsed_url.has_host())
.map(|parsed_url| parsed_url.into_string())
.collect();
This is possible now with ParallelBridge
:
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;
use std::sync::mpsc::channel;
let rx = {
let (tx, rx) = channel();
tx.send("one!");
tx.send("two!");
tx.send("three!");
rx
};
let mut output: Vec<&'static str> = rx.into_iter().par_bridge().collect();
output.sort_unstable();
assert_eq!(&*output, &["one!", "three!", "two!"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With