currently I am trying to learn rust. I wanted to create a very little and simple todo list, but I have some issues with matching the user input to the different options.
I've allready tried different solutions from the Internet, but the program allways ends up in the "no match" branch of my code:
use std::io::{self, stdin};
fn main() {
let mut todo = Vec::new();
println!("--TODO LIST--");
println!("");
println!("1. Show List");
println!("2. Add to list");
println!("3. Remove from list");
let mut input = String::new();
let mut add = String::new();
stdin().read_line(&mut input).expect("cannot readline");
let mut i = 0;
let _= input.trim_end();
//println!();
//println!("{}", input);
match input.as_str() {
"1" => {
while i <= todo.len() {
print!("{}. {}", i, todo[i]);
i += 1;
}
},
"2" => {
println!("What do you want to add?");
io::stdin().read_line(&mut add).expect("Invalid answer");
todo.push( add);
},
"3" => {
println!("What do you want to remove (number)");
io::stdin().read_line(&mut add).expect("Invalid answer");
let my_int = add.parse::<usize>().unwrap();
todo.remove(my_int);
},
_=> {
println!("No Match");
main();
},
};
main();
}
let _= input.trim_end();
It seems you expected trim_end()
to mutate the string in place. But it does not; it takes a &str
and returns a &str
which is a slice of the original, with whitespaces at the end removed.
So:
match input.trim_end() {
// ...
}