如何从输入中读取单个字符作为u8?

pen*_*123 9 stdin input rust

我目前正在为这种语言建立一个简单的口译员来练习.唯一需要克服的问题是从用户输入中读取单个字节作为字符.到目前为止,我有以下代码,但我需要一种方法String将第二行生成的转换为u8我可以转换的一个或另一个整数:

let input = String::new()
let string = std::io::stdin().read_line(&mut input).ok().expect("Failed to read line");
let bytes = string.chars().nth(0) // Turn this to byte?
Run Code Online (Sandbox Code Playgroud)

以字节为单位的值应该是u8我可以转换为a i32以在其他地方使用的值.也许有一种更简单的方法可以做到这一点,否则我将使用任何有效的解决方案.

A.B*_*.B. 14

只读一个字节并将其转换为i32:

use std::io::Read;

let input: Option<i32> = std::io::stdin()
    .bytes() 
    .next()
    .and_then(|result| result.ok())
    .map(|byte| byte as i32);

println!("{:?}", input);
Run Code Online (Sandbox Code Playgroud)