Skip to content

Instantly share code, notes, and snippets.

@buyoh
Created April 5, 2019 22:58
Show Gist options
  • Save buyoh/0373110f01e4dd509cb571f75698c93a to your computer and use it in GitHub Desktop.
Save buyoh/0373110f01e4dd509cb571f75698c93a to your computer and use it in GitHub Desktop.
/// 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