为什么我的字符串在阅读stdin的用户输入时不匹配?

发布于 2025-01-29 02:23:23 字数 445 浏览 6 评论 0 原文

我正在尝试获取用户输入,并检查用户是否放入“ y”或“ n”。令人惊讶的是,在以下代码中,如果> case case执行!显然, recript_name 既不是“ y”也不是“ n”。那怎么可能?我是在做错了弦转换吗?

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!");
    }
}

I'm trying to get user input and check if the user put in "y" or "n". Surprisingly, in the below code, neither the if nor the if else case executes! Apparently, correct_name is neither "y" nor "n". How can that be? Am I doing my string conversion wrong or something?

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!");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2025-02-05 02:23:23

read_line 在返回的字符串中包含终止的newline。
要删除它,请使用 trim_end 甚至更好,只需 trim trim

use std::io;

fn main() {
    let mut correct_name = String::new();
    io::stdin()
        .read_line(&mut correct_name)
        .expect("Failed to read line");

    let correct_name = correct_name.trim();

    if correct_name == "y" {
        println!("matched y!");
    } else if correct_name == "n" {
        println!("matched n!");
    }
}

最后一个案例处理许多类型的白期:

返回一个带有领先和尾随空间的弦乐片。

'whitespace'是根据Unicode派生的核心属性的术语White_space定义的。

Windows / Linux / MacOS不重要。


您也可以使用修剪结果的长度来截断原始的字符串,但是在这种情况下,您应仅使用 trim_end

let trimmed_len = correct_name.trim_end().len();
correct_name.truncate(trimmed_len);

read_line includes the terminating newline in the returned string.
To remove it, use trim_end or even better, just trim:

use std::io;

fn main() {
    let mut correct_name = String::new();
    io::stdin()
        .read_line(&mut correct_name)
        .expect("Failed to read line");

    let correct_name = correct_name.trim();

    if correct_name == "y" {
        println!("matched y!");
    } else if correct_name == "n" {
        println!("matched n!");
    }
}

This last case handles lots of types of whitespace:

Returns a string slice with leading and trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

Windows / Linux / macOS shouldn't matter.


You could also use the trimmed result's length to truncate the original String, but in this case you should only use trim_end!

let trimmed_len = correct_name.trim_end().len();
correct_name.truncate(trimmed_len);
佞臣 2025-02-05 02:23:23

read_line 在返回的字符串中包含终止的newline。将 .trim_right_matches(“ \ r \ n”)添加到您的定义 recripe_name 以删除终端newline。

read_line includes the terminating newline in the returned string. Add .trim_right_matches("\r\n") to your definition of correct_name to remove the terminating newline.

献世佛 2025-02-05 02:23:23

您可以使用 chomp-nl crate 提供字符串切片没有新线字符。

还有一个 trait trait /a>如果您愿意就位执行此操作。

免责声明:我是这个图书馆的作者。

You can use the chomp-nl crate which provides a chomp function which returns a string slice without the newline characters.

There is also a trait ChompInPlace if you prefer to do this in-place.

Disclaimer: I am the author of this library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文