Logo

Programming-Idioms

History of Idiom 13 > diff from v5 to v6

Edit summary for version 6 by :

Version 5

2015-08-19, 07:39:48

Version 6

2015-08-19, 14:11:55

Idiom #13 Iterate over map keys and values

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

Idiom #13 Iterate over map keys and values

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

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);`.

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
http://is.gd/FnGX0Z