use chain::{ Chain, }; use std::{ io::{ BufRead, self, }, }; fn buffered_read_all_lines io::Result<()>>(input: &mut T, mut then: F) -> io::Result { let mut buffer = String::new(); let mut read; let mut total=0; while {read = input.read_line(&mut buffer)?; read!=0} { then(&buffer[..])?; buffer.clear(); total += read; } Ok(total) } fn main() { let stdin = io::stdin(); let mut stdin = stdin.lock(); let mut chain = Chain::new(); buffered_read_all_lines(&mut stdin, |string| { chain.feed(&string.split_whitespace() .filter(|word| !word.is_empty()) .map(|s| s.to_owned()).collect::>()); Ok(()) }).expect("Failed to read from stdin"); if !chain.is_empty() { if let Some(num) = std::env::args().skip(1).next() { let sz: usize = num.parse().expect("Cannot parse number of tokens to generate"); for string in chain.str_iter_for(sz) { println!("{}", string); } } else { println!("{}", chain.generate_str()); } } }