Associative array/Iteration: Difference between revisions

Content added Content deleted
(→‎{{header|TXR}}: Dop @(do ...), remove spurious argument in [h k nil], streamline.)
(→‎{{header|Rust}}: Made example match the creating assiociated array example)
Line 2,135:
=={{header|Rust}}==
<lang Rust>use std::collections::HashMap;
 
fn main() {
let mut squaresolympic_medals = HashMap::new();
squaresolympic_medals.insert("oneUnited States", 1(1072, 859, 749));
squaresolympic_medals.insert("twoSoviet Union", 4(473, 376, 355));
squaresolympic_medals.insert("threeGreat Britain", 9(246, 276, 284));
olympic_medals.insert("Germany", (252, 260, 270));
for key in squares.keys() {
for (country, medals) in olympic_medals {
println!("Key {}", key);
println!("{} has had {} gold medals, {} silver medals, and {} bronze medals",
}
country, medals.0, medals.1, medals.2);
for value in squares.values() {
println!("Value {}", value);
}
for (key, value) in squares.iter() {
println!("{} => {}", key, value);
}
}</lang>
{{out}}
Note that <code>HashMap</code> does not preserve order (if this is important, <code>std::collections::BTreeMap</code> is what you want.)
<pre>
Germany has had 252 gold medals, 260 silver medals, and 270 bronze medals
Key three
United States has had 1072 gold medals, 859 silver medals, and 749 bronze medals
Key two
Soviet Union has had 473 gold medals, 376 silver medals, and 355 bronze medals
Key one
Great Britain has had 246 gold medals, 276 silver medals, and 284 bronze medals
Value 9
Value 4
Value 1
three => 9
two => 4
one => 1
</pre>