Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hkBst opened this issue Jan 2, 2025 · 0 comments
Open

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

hkBst opened this issue Jan 2, 2025 · 0 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@hkBst
Copy link
Contributor

hkBst commented Jan 2, 2025

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.

@hkBst hkBst added the C-bug Category: This is a bug. label Jan 2, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants