I can straight-forwardly match a String
in Rust:
let a = \"hello\".to_string();
match &a[..] {
\"hello\" => {
println!(\"Mat
In some cases, you can use unwrap_or
to replace Option::None
with a predefined &str
you don't want to handle in any special way.
I used this to handle user inputs:
let args: Vec = env::args().collect();
match args.get(1).unwrap_or(&format!("_")).as_str() {
"new" => {
print!("new");
}
_ => {
print!("unknown string");
}
};
Or to match your code:
let option = Some("hello");
match option.unwrap_or(&format!("unhandled string").as_str()) {
"hello" => {
println!("hello");
}
_ => {
println!("unknown string");
}
};