Generic puzzle with AsRef<str> + PartialEq


I have this example:

I know I can make it work by changing fct to take &T instead of &String but it is to demonstrate the issue I'm facing where what I pass to contains is a &String.

String implement both traits bounds I set on T so I'm a bit lost on what's happening here :-/

Since the elements of self.f implement AsRef<str>, you can call the .as_ref() method on them to borrow them as &str and compare them to strings:

    pub fn fct(&self, p: &String) {
        println!("{}", self.f.iter().any(|x| x.as_ref() == p));


1 Like