Skip to content

Better doctests for HashMap's into_values and into_keys methods #87591

Closed
@ChaiTRex

Description

@ChaiTRex

The current doctests for a few methods don't really test anything when they could. The following changes would:

  • improve the documentation for programmers by better showing what the results of the functions are
  • bring the documentation in line with the equivalent methods for BTreeMap, which do include tests like the following
  • act as two free tests of those methods

The current doctest for HashMap's into_keys method can be improved to:

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

let mut vec: Vec<&str> = map.into_keys().collect();
vec.sort_unstable();
assert_eq!(vec, ["a", "b", "c"]);

The current doctest for HashMap's into_values method can be improved to:

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

let mut vec: Vec<i32> = map.into_values().collect();
vec.sort_unstable();
assert_eq!(vec, [1, 2, 3]);

I would submit a pull request, but my computer is broken. Could someone please make these changes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions