Created
April 5, 2019 22:58
-
-
Save buyoh/0373110f01e4dd509cb571f75698c93a 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
/// 1文字読む. | |
/// let c = getc(&mut std::io::stdin()).unwrap(); | |
fn getc(r : &mut Read) -> Result<u8, Error>{ | |
let mut buf = [0;1]; | |
match r.read(&mut buf){ | |
Ok(size) => { if size > 0 { Ok(buf[0]) } else { Err(Error::new(ErrorKind::Other, "no out") ) } } | |
Err(err) => { Err(err) } | |
} | |
} | |
#[test] | |
fn test_getc() { | |
let text = "ab"; | |
let mut buf = std::io::BufReader::new(text.as_bytes()); | |
assert_eq!(97, getc(&mut buf).unwrap()); | |
assert_eq!(98, getc(&mut buf).unwrap()); | |
assert_eq!(true, getc(&mut buf).is_err()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment