Why does `str::contains` take a pattern but `slice::contains` takes an exact element?

I need to find out if a slice contains an element that satisfies some condition. I know that str::contains takes a Pattern, so I at first tried this:

my_list.contains(|el| my_condition(el)); // yes, I know this particular case can be written point-free

but that doesn't work since slice::contains takes an &T. Why is that and what should I do instead?

1 Like

I’ll leave the history lesson about how the API came to be to others, but something like this should solve your problem (untested):

my_list.iter().any(|el| my_condition(el));
1 Like

You can usually just write my_list.iter().any(my_condition) instead (but there are cases where you can't).