Logo

Programming-Idioms

History of Idiom 13 > diff from v77 to v78

Edit summary for version 78 by programming-idioms.org:
[Rust] Avoid link shortener if possible

Version 77

2020-02-13, 19:59:59

Version 78

2020-03-20, 18:00:25

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Illustration

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Illustration
Extra Keywords
table dictionary hash traverse traversal
Extra Keywords
table dictionary hash traverse traversal
Imports
use std::collections::BTreeMap;
Imports
use std::collections::BTreeMap;
Code
for (key, val) in &mymap {
    println!("Key={key}, Value={val}", key=key, val=val);
}
Code
for (key, val) in &mymap {
    println!("Key={key}, Value={val}", key=key, val=val);
}
Comments bubble
You can also print collections in a 'nice' way with `println!("{:?}", mymap);` which is the Debug-representation. You can also use "{:#?}" for the pretty version.

This example works the same if you replace `BTreeMap` with `HashMap`.
Comments bubble
You can also print collections in a 'nice' way with `println!("{:?}", mymap);` which is the Debug-representation. You can also use "{:#?}" for the pretty version.

This example works the same if you replace `BTreeMap` with `HashMap`.
Demo URL
http://is.gd/FnGX0Z
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=fb834bea91b3e613711256344e0cf289