17

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!");
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Joe
  • 355
  • 3
  • 8

3 Answers3

23

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);
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
14

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
  • What about Windows, should it be "\r\n"? – ArtemGr Jan 05 '15 at 11:10
  • It tells me bool` is not implemented for the type `&str when I add that – Joe Jan 05 '15 at 11:53
  • I realized it has to go in the if statement after I looked at docs. Thanks! – Joe Jan 05 '15 at 12:07
  • @ArtemGr That sounds right, but I don't know enough about Windows to be sure. – wingedsubmariner Jan 05 '15 at 14:33
  • 2
    You should not need `as_slice`, since `String` implements `Deref`. `let line = io::stdin().read_line(); let trimmed = line.trim_right_chars(..);` should work. – reem Jan 05 '15 at 16:58
  • 3
    And I'd suggest using just [`trim`](http://doc.rust-lang.org/std/str/trait.StrExt.html#tymethod.trim) or [`trim_right`](http://doc.rust-lang.org/std/str/trait.StrExt.html#tymethod.trim_right) instead of trying to worry about cross-platform newlines :-) – Shepmaster Jan 05 '15 at 22:14
  • @Shepmaster `trim_right` would also eat other trailing whitespace, which might not be a good thing. – wingedsubmariner Jan 06 '15 at 17:55
  • This was changed to [`trim_end`](https://doc.rust-lang.org/std/primitive.str.html#method.trim_end)/[`trim_end_matches`](https://doc.rust-lang.org/std/primitive.str.html#method.trim_end_matches) in 1.33 – kmdreko Jul 10 '21 at 21:03
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.

antoyo
  • 11,097
  • 7
  • 51
  • 82
  • What benefit / difference is there from `trim` / `trim_right` / `trim_right_matches`? – Shepmaster Nov 15 '16 at 18:15
  • 1
    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. – antoyo Nov 15 '16 at 19:21