相关疑难解决方法(0)

为什么我的stdin用户输入没有正确匹配?

我正在尝试获取系统输入并检查用户是否输入了是或否.我的字符串转换是错误还是什么?if块不执行.

use std::io;

fn main() {
    let mut correct_name = String::new();
    io::stdin().read_line(&mut correct_name).expect("Failed to read line");
    if correct_name == "y" {
        println!("matched y!");
    } else if correct_name == "n" {
        println!("matched n!");
    }
}
Run Code Online (Sandbox Code Playgroud)

rust

8
推荐指数
2
解决办法
1646
查看次数

标签 统计

rust ×1