'I tried to get the user input and then compare it to somthing and i got a problem [duplicate]
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!");
}
}
}
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jh316 |
