Generic puzzle with AsRef<str> + PartialEq

Hi,

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));
    }

Playground

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.