String, &str match multiple patterns?


Hi i want to match multiple string example something like

let st : String = “Test”.to_string();

match &st {
“Test” => { Do something }
“Test2” => { Do something 2}
_ => panic!(…)

but this doesn’t work… if i try below it works no errors:
match st {
_ => println(“Some string found!”)

help !

Thanks in advance


See for a working version.

In your first example, the type of the expression being matched is &String but the type of the cases is &str.

In your second example, the type of the case is &String which is why it worked.


You can do:

match st.as_str() {`

(match &*st also works, but I personally find it less clear)