Rust 不比较生锈的字符串
我从用户那里获取了输入,但当我尝试比较时,它不会输入if或else if块。相反,它转到else块Rust 不比较生锈的字符串,rust,Rust,我从用户那里获取了输入,但当我尝试比较时,它不会输入if或else if块。相反,它转到else块 use std::io; fn main() { let small_coffee = 2; let medium_coffee = 4; let large_coffee = 6; let mut total_price = 0; let mut user_choice = String::new(); io::stdin().read_li
use std::io;
fn main() {
let small_coffee = 2;
let medium_coffee = 4;
let large_coffee = 6;
let mut total_price = 0;
let mut user_choice = String::new();
io::stdin().read_line(&mut user_choice).ok().expect("Eror");
if user_choice == "1" {
total_price += small_coffee;
} else if user_choice == "2" {
total_price += medium_coffee;
} else if user_choice == "3" {
total_price += large_coffee;
} else {
println!("Sorry! Invalid Choice");
}
println!("Your total bill amounted to : {}$", total_price);
}
此函数将从基础流中读取字节,直到找到换行符(0xA字节)或EOF为止。一旦找到,分隔符(如果找到)之前的所有字节(包括)都将附加到buf。
使用,
let input=user_choice.trim()代码>谢谢兄弟抽出时间回答这个问题。但是我在Ubuntu 18.04上使用VSCode时遇到了问题