I'm trying to write a case-insensitive string type, and I'm having problems with match arms. It seems that match doesn't use PartialEq under the hood, but I couldn't find out if there's another trait that it uses. (Most results for "match" + "trait" are about matching on traits, which is not what I'm interested in here).
Here's a minimal example:
#[derive(Debug)] pub struct CaseInsensitiveStr<'a>(&'a str); impl<'a> CaseInsensitiveStr<'a> { #[inline] pub const fn new(s: &'a str) -> CaseInsensitiveStr { CaseInsensitiveStr(s) } } impl<'a, S: AsRef<str>> PartialEq<S> for CaseInsensitiveStr<'a> { #[inline] fn eq(&self, other: &S) -> bool { self.0.eq_ignore_ascii_case(other.as_ref()) } } fn main() { let a = CaseInsensitiveStr::new("FOO"); if a == "foo" { println!("eq works"); } match a { "foo" => println!("matched"), _ => println!("no match"), }; } which gives the following compiler error:
error[E0308]: mismatched types --> src/main.rs:26:9 | 25 | match a { | - this expression has type `CaseInsensitiveStr<'_>` 26 | "foo" => println!("matched"), | ^^^^^ expected `CaseInsensitiveStr<'_>`, found `&str` | help: try wrapping the pattern in `CaseInsensitiveStr` | 26 | CaseInsensitiveStr("foo") => println!("matched"), | +++++++++++++++++++ + help: you might have meant to use field `0` whose type is `&str` | 25 | match a.0 { | ++ I certainly don't want to use a.0 and CaseInsensitiveStr("foo") compiles but doesn't actually do the correct thing (I think it's just essentially doing a.0?).
CaseInsensitiveStr::new("foo") is also invalid here, which I suspect also means any kind of macro tricky in a plain match statement isn't going to work either.
Is there a trait that I'm missing, or is it just not possible to get match to like this?
1 post - 1 participant