slice::contains is not general in the key like HashMap::get
Open
Nobody has claimed this yet.
C-discussion
T-libs
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the slice::contains and HashMap::get entry points, using the reproducer in the issue to compare their accepted key types. Determine the intended generalization for slices and verify that the &str lookup works without breaking existing contains calls.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100