slice::contains is not general in the key like HashMap::get #135019
Open
Description
I tried this code:
fn main() {
let words = ["hello", "world"].map(|s| s.to_string());
// this works fine
let hm: std::collections::HashMap<String, usize> = words
.iter()
.cloned()
.zip(words.iter().map(|w| w.len()))
.collect();
assert_eq!(hm.get("hello"), Some(&5));
// this does NOT work
assert!(words.contains("hello"));
// but has this simple workaround
assert!(words.iter().any(|e| e == "hello"));
}
I expected to see this happen: works
Instead, this happened: slice::contains does not accept a &str
instead of a &String
, like HashMap::get does. I tried searching for older issues to see if there may be a good reason for this, but I did not find anything.