How to match a String against string literals in rust?
In rust we can usematch
syntax over string as_srt()
function and we provide the conditions inside the match. See below example:fn main() {
let stringthing = String::from("c");
match stringthing.as_str() {
"a" => println!("10"),
"b" => println!("20"),
"c" => println!("30"),
_ => println!("something else!"),
}
}
Output
30