How to read a single character from input as u8?

后端 未结 2 1691
误落风尘
误落风尘 2021-01-17 13:56

I am currently building a simple interpreter for this language for practice. The only problem left to overcome is reading a single byte as a character from user input. I hav

2条回答
  •  自闭症患者
    2021-01-17 14:41

    Reading just a byte and casting it to i32:

    use std::io::Read;
    
    let input: Option = std::io::stdin()
        .bytes() 
        .next()
        .and_then(|result| result.ok())
        .map(|byte| byte as i32);
    
    println!("{:?}", input);
    

提交回复
热议问题