Skip to content

Feature Request: count_by under no_std #1092

Description

@fxdmhtt

Thanks to the existence of the hashbrown crate, it is possible to provide no_std support for HashMap—provided that alloc is enabled.

The code itself remains unchanged; only the import statements need to be modified.

use core::hash::Hash;

use hashbrown::HashMap;

pub trait Counts: Iterator {
    fn counts(self) -> HashMap<Self::Item, usize>
    where
        Self: Sized,
        Self::Item: Eq + Hash,
    {
        let mut counts = HashMap::new();
        self.for_each(|item| *counts.entry(item).or_default() += 1);
        counts
    }
}

impl<I: Iterator> Counts for I {}

Would you consider adding this support? Doing so would broaden the applicability of the APIs related to HashMap and HashSet.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions