It’s a known limitation of Rust’s patterns.
Method calls (including internal methods for operators like ==) automatically call .deref() as needed, so String gets automagically turned into &str for comparisons with literals.
On the other hand, the patterns are quite literal in their comparisons, and find that String and &str are different.
There are two solutions:
-
Change
Option<String>toOption<&str>before matching on it:Some(a).as_deref(). Theas_deref()is a combo ofas_ref()that makesOption<&String>(preventing move), andderef()/as_str()then unambiguously references it as a&str. -
Use match guard:
match Some(a) { Some(ref s) if s == "hello" => … }.Some(ref s)matches anyString, and captures it ass: &String, which you can then compare in theifguard which does the usual flexible coercions to make it work.
See also:
- Converting from Option<String> to Option<&str>