I have a function that can take a &[&str]
or a &[String]
and return different values based on the slice. It works fine using ==
if I add a PartialEq
constraint:
pub fn info1<'a, S>(path: &[S]) -> Option<String>
where
S: PartialEq<&'a str>,
{
if path == ["a", "b"] {
return Some("It's a-b!".to_string())
}
None
}
But it doesn't work if I use match
:
pub fn info2<'a, S>(path: &[S]) -> Option<String>
where
S: PartialEq<&'a str>,
{
match path {
["a", "b"] => Some("It's a b!".to_string()),
_ => None,
}
}
error[E0308]: mismatched types
--> src/lib.rs:16:6
|
11 | pub fn info2<'a, S>(path: &[S]) -> Option<String>
| - this type parameter
...
15 | match path {
| ---- this expression has type `&[S]`
16 | ["a", "b"] => Some("It's a b!".to_string()),
| ^^^ expected type parameter `S`, found `&str`
|
= note: expected type parameter `S`
found reference `&'static str`
Is there any way to make it work? Ideally without requiring something like ["a".as_foo(), "b".as_foo()]
.