Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my string not match when reading user input from stdin?

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!");
    }
}
like image 665
Joe Avatar asked Jan 05 '15 04:01

Joe


3 Answers

Instead of trim_end_matches (previously called trim_right_matches), I'd recommend using trim_end (previously called trim_right) 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);
like image 197
Shepmaster Avatar answered Oct 04 '22 01:10

Shepmaster


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.

like image 27
wingedsubmariner Avatar answered Oct 03 '22 23:10

wingedsubmariner


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.

like image 40
antoyo Avatar answered Oct 04 '22 00:10

antoyo