Skip to content

slice::contains is not general in the key like HashMap::get #135019

Open
@hkBst

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.

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions