Vec::contains should accept anything that T implements Borrow for

Vec::contains accepts &T. HashSet::contains is more general: it accepts any &Q where T: Borrow<Q>.

This makes it inconvenient to use Vec::contains on a Vec<Vec<u8>> with static bytestring slices (and I assume similarly for Vec<String>.

Would you consider generalizing Vec::contains to accept the more general type accepted by HashSet::contains?

9 Likes

(Looks like the next step is a PR.)

1 Like

I just bumped into this today. Iā€™ve got a Vec<String>, and Iā€™d like to use .contains("static string") instead of having to do something like .iter().any(|v| v == "static_string"). So a big thumbs up for this!

People have asked for this before. It hasnā€™t happened yet because thatā€™s a breaking change in cases where inference determines the concrete type of T. I agree itā€™s a bit ridiculous that we canā€™t use .contains("foo") on a Vec<String>, but yeah. See https://github.com/rust-lang/rust/pull/37761

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.