Logo

Programming-Idioms

History of Idiom 113 > diff from v29 to v30

Edit summary for version 30 by programming-idioms.org:
[Rust] Variable names mymap, x

Version 29

2020-04-29, 09:36:39

Version 30

2020-05-03, 19:53:03

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Variables
k,x,mymap
Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Code
let mut items: Vec<_> = map.iter().collect();
items.sort_by_key(|item| item.1);
for (k, v) in items {
    println!("[{},{}]", k, v);
}
Code
let mut items: Vec<_> = mymap.iter().collect();
items.sort_by_key(|item| item.1);
for (k, x) in items {
    println!("[{},{}]", k, x);
}
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2523003142f55a5403061cbbc7ad028c
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=34dc5efca6080ba5cc6c942e0113dc46