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

#1

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

#2

(Looks like the next step is a PR.)

0 Likes

#3
1 Like

#4

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!

0 Likes

#5

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

closed #6

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

0 Likes