std-method

HashSet::contains

Check membership by borrowing a query compatible with the stored element type. A set of String values can be queried by str. Expected stdout (not executed): true false Common mistake: Allocating a String unnecessarily to check a literal.

Formal Syntax & Structure

rust syntaxrust
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where T: Borrow<Q>, Q: Hash + Eq

Working Example

Example Snippetrust
fn main() {    use std::collections::HashSet;    let allowed = HashSet::from([String::from("Neda")]);    println!("{} {}", allowed.contains("Neda"), allowed.contains("Ali"));}
Rust Reference