18

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

3 Answers 3

25

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);
Sign up to request clarification or add additional context in comments.

Comments

15

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.

8 Comments

What about Windows, should it be "\r\n"?
It tells me bool` is not implemented for the type `&str when I add that
I realized it has to go in the if statement after I looked at docs. Thanks!
You should not need as_slice, since String implements Deref<Target=str>. let line = io::stdin().read_line(); let trimmed = line.trim_right_chars(..); should work.
And I'd suggest using just trim or trim_right instead of trying to worry about cross-platform newlines :-)
|
1

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.

2 Comments

What benefit / difference is there from trim / trim_right / trim_right_matches?
This is using the trim_right_matches function so the chomp function is a shortcut. There is also a trait for doing in-place trimming using truncate thought. So the benefit is mainly to have a shorter way to trim newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.