Skip to content

from_iter on heapless::String should be generic over any iterator that yields a type that implements AsRef<str> #630

Description

@redzic

You currently cannot use from_iter directly with a &[CompactString] for example. You have to do .into_iter().map(...) which is annoying. I ended up building my own helper function for this but it would be nicer if the heapless crate itself provided this generic impl.

This is my helper function:

pub fn build_hstring<const N: usize, I, S>(iter: I) -> heapless::String<N>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let mut new = heapless::String::<N>::new();
    for c in iter {
        new.push_str(c.as_ref()).expect("build_hstring: capacity exceeded");
    }
    new
}

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