From 55b07581cf2e414460461e9e1591059b5dc459d3 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 5 Apr 2016 21:05:34 -0400 Subject: Read stdin Tried a few different times, copying code from different places. Didn't quite get it figured out at first. Wanted to add STDIN lines to a string. Got that working with option 3, but that `push_str` didn't appear to preserve newlines. Ended up using this solution from StackOverflow: http://stackoverflow.com/questions/13579266/how-to-read-user-input-in-rust/36374135#36374135 It adds the lines to a vector instead of a single string. Printing each line individually then gives us out newlines. This way is probably nicer also for getting the first line that starts with "From: ". --- src/main.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b7fbd08..135e0c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,33 @@ +use std::io::{self, BufRead}; +// use std::io::Read; + fn main() { + // let mut input = String::new(); + // io::stdin().read_to_end(&mut input).unwrap(); + // .expect("Failed to read STDIN"); + + // match io::stdin().lock() { + // Ok(s) => s.read_to_string(&mut input), + // Err(e) => panic!("Error: {}", e), + // } + + // let stdin = io::stdin(); + // for line in stdin.lock().lines() { + // input.push_str(&line.unwrap()); + // } + + // let stdin = io::stdin(); + // loop { + // stdin.read_line(&mut input).expect("asdf"); + // } + + let stdin = io::stdin(); + let input: Vec = stdin.lock().lines().map(|line| line.unwrap()).collect(); + + for l in &input { + println!("{}", l); + } + // if alias not new // do nothing // if alias is new -- cgit v1.2.3