0

I tried to get the user input and then compare it to somthing and i got a problem :(

this is my code:

use std::io::stdin;

fn main() {
    let mut command = String::new();
    loop {
        stdin().read_line(&mut command).ok().expect("Failed to read line");
        if String::from("help") == &*command {
            println!("it worked!");
        }
    }
}
Elad87
  • 941
  • 1
  • 3
  • 8

1 Answers1

1
use std::io::stdin;

fn main() {
    let mut command = String::new();
    loop {
        stdin().read_line(&mut command).ok().expect("Failed to read line");
        if String::from("help") == command.trim_end().to_string() {
            println!("it worked!");
        }
        command.clear();
    }
}

Try clearing the string before you read it again in the loop. Also, read_line leaves the newline in the string, so you may want to trim the end of the string before you compare.

jh316
  • 901
  • 7
  • 9