【问题标题】:Not comparing string in Rust [duplicate]不比较 Rust 中的字符串 [重复]
【发布时间】:2019-12-22 16:17:48
【问题描述】:

我已经接受了用户的输入,但是当我尝试比较时,它没有进入 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_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);
}

【问题讨论】:

  • 感谢兄弟抽出时间回答问题。 . .但我在 Ubuntu 18.04 上使用 VSCode 时遇到了问题

标签: rust


【解决方案1】:

此函数将从底层流中读取字节,直到找到换行符分隔符(0xA 字节)或 EOF。找到后,直到并包括 分隔符(如果找到)的所有字节都将附加到 buf。

使用trim()let input = user_choice.trim();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 2022-11-15
    • 1970-01-01
    • 2014-12-08
    • 2017-04-10
    • 2012-01-11
    • 2011-07-14
    • 1970-01-01
    相关资源
    最近更新 更多