I'm trying to read a stream of JSON objects line-by-line from stdin and extract the "value" key from them as a string:
use rayon::prelude::*; // 1.0.3
use serde_json::Value; // 1.0.37
use std::io::{self, BufRead};
use std::sync::mpsc::channel;
fn main() {
let stdin = io::stdin();
let rx = {
let (tx, rx) = channel();
for line in stdin.lock().lines() {
tx.send(line.unwrap()).unwrap();
}
rx
};
let it = rx.into_iter().par_bridge().map(|line| -> String {
let v: Value = serde_json::from_str(&line.clone()).unwrap();
let ret = v["value"].as_str().unwrap().into();
println!("{}", ret);
ret
});
eprintln!("Starting actual work.");
it.for_each(|x| {
println!("{}", x);
});
}
eprintln!()
is never called and I run out of memory because I use a huge file as input. Why doesn't map()
just return an iterator I could use but instead waits for its input to end?