How to check if HashMap contains specific key in Rust

1 Answer

0 votes
use std::collections::HashMap;

fn main() {
    let hm = HashMap::from([
        ("Rust", 3.8),
        ("C++", 2.1),
        ("Java", 8.3),
        ("C", 19.7),
    ]);

    if hm.contains_key(&"Rust") {
        println!("found");
    } else {
        println!("not found");
    }
}


 
 
/*
run:
 
found
 
*/

 



answered Oct 29, 2022 by avibootz

Related questions

...