How to iterate over map keys and values in Rust

1 Answer

0 votes
use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    
    map.insert("Rust", 1);
    map.insert("Python", 2);
    map.insert("C", 3);

    // Iterate over keys and values
    for (key, value) in &map {
        println!("Key: {} Value: {}", key, value);
    }
}



/*
run:

Key: Python Value: 2
Key: Rust Value: 1
Key: C Value: 3

*/

 



answered Apr 19, 2025 by avibootz

Related questions

1 answer 151 views
1 answer 91 views
1 answer 98 views
2 answers 109 views
1 answer 109 views
1 answer 107 views
...