Skip to content

Unclear documentation around HashMap iteration order #89040

@e00E

Description

@e00E

The documentation for HashMap on the methods keys, values, iter, etc is phrased

An iterator visiting all <type> in arbitrary order.

The std::collections module documentation states

For unordered collections like HashMap, the items will be yielded in whatever order the internal representation made most convenient.

To me this makes it clear that I cannot expect any specific iteration order. What is not clear is whether the iteration order is persistent across multiple calls to the same HashMap without modification in between.
Are these two loops guaranteed to iterate in the same order?

for _ in hashmap.iter() {}
for _ in hashmap.iter() {}

Or these?

let iter = hashmap.iter();
for _ in iter.clone() {}
for _ in iter.clone() {}

Erring on the side of caution I would say that no the iteration order is not guaranteed to be the same.

The documentation should clarify this. I don't know whether the iteration order should be guaranteed in this case but either way it should be clarified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-help-wantedCall for participation: Help is requested to fix this issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions