Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Rust: Added looping through only keys
make Rust example runnable (missing import), reorder iteration examples
Line 1,674:
 
<syntaxhighlight lang="text">
use std::collections::HashMap;
let mut phone_book = HashMap::new();
phone_book.insert("Sally Smart", "555-9999");
Line 1,680 ⟶ 1,681:
</syntaxhighlight>
 
The default iterators visit all entries as tuples in an unspecified order:
Loop iterating through all the keys of the hash map:
<syntaxhighlight lang="text">
for (name, number) in &phone_book.keys() {
println!("{} {}", name, number);
}
</syntaxhighlight>
 
Loop iteratingIterating through all the keys of the hash map:
To loop hrough all entries as tuples:
<syntaxhighlight lang="text">
for (name, number) in &phone_book.keys() {
println!("{} {}", name, number);
}
</syntaxhighlight>