Content deleted Content added
make Rust example runnable (missing import), reorder iteration examples |
probably worth mentioning BTreeMap in Rust section as well |
||
Line 1,671:
===Rust===
The [[Rust (programming language)|Rust]] standard library provides a hash map (<code>std::collections::HashMap</code> and a [[B-tree]] map (<code>std::collections::BTreeMap</code>). They share several methods with the same names, but have different requirements for the types of keys that can be inserted. The <code>HashMap</code> requires keys to implement the <code>Eq</code> ([[equivalence relation]]) and <code>Hash</code> (hashability) traits and it stores entries in an unspecified order, and the <code>BTreeMap</code> requires the <code>Ord</code> trait for its keys and it stores entries in an order defined by the key type. The order is reflected by the default iterators.
<syntaxhighlight lang="text">
Line 1,681:
</syntaxhighlight>
The default iterators visit all entries as tuples. The <code>HashMap</code> iterators visit entries in an unspecified order
<syntaxhighlight lang="text">
for (name, number) in &phone_book {
Line 1,688:
</syntaxhighlight>
There is also an iterator for keys:
<syntaxhighlight lang="text">
for name in phone_book.keys() {
|