Created
June 27, 2014 15:20
-
-
Save krdln/af7c0adf16e7c1383c1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::IoResult; | |
struct SBuf<T> { | |
br: T, | |
current: String | |
} | |
impl<T: Buffer> SBuf<T> { | |
fn new(br: T) -> SBuf<T> { | |
SBuf { | |
br: br, | |
current: String::new() | |
} | |
} | |
fn line<'b>(&'b mut self) -> Option<IoResult<&'b str>> { | |
use std::io::{IoError,EndOfFile}; | |
self.current.clear(); | |
loop { | |
match self.br.read_char() { | |
Ok('\n') => break, | |
Ok(c) => self.current.push_char(c), | |
Err(IoError{kind: std::io::EndOfFile, ..}) => return None, | |
Err(err) => return Some(Err(err)) | |
} | |
} | |
Some(Ok( self.current.as_slice() )) | |
} | |
fn next<'b>(&'b mut self) -> Option<IoResult<&'b str>> { self.line() } | |
} | |
trait BufExtensions { | |
fn sbuf(self) -> SBuf<Self>; | |
} | |
impl<B: Buffer> BufExtensions for B { | |
fn sbuf(self) -> SBuf<B> { | |
SBuf::new(self) | |
} | |
} | |
fn main() { | |
let bri = std::io::BufferedReader::new(std::io::stdin()); | |
let mut sbuf = bri.sbuf(); | |
let mut sum = 0u; | |
// ~let mut my_stdout = std::io::stdio::stdout(); | |
for x in sbuf { | |
sum += from_str( x.unwrap().trim() ).unwrap() | |
} | |
println!("{}", sum); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut br = std::io::BufferedReader::new(std::io::stdin()); | |
let mut sum = 0u; | |
// ~let mut my_stdout = std::io::stdio::stdout(); | |
for x in br.lines() { | |
match x { | |
Ok(x) => sum += from_str( x.as_slice().trim() ).unwrap(), | |
_ => break | |
} | |
} | |
println!("{}", sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment