Skip to content

Add FromIterator impls for ascii::Chars to Strings #141445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yotamofek
Copy link
Contributor

Wanted to collect ascii chars into a String while working on #141369 , and was surprised these impls don't exist. Seems to me to be simply oversight.

BTW, I only added impl FromIterator<ascii::Char> for Cow<'_, str>, without a corresponding FromIterator<&Char> impl, because there's no existing impl for FromIterator<&char>, but that might be oversight too.

cc #110998

@rustbot
Copy link
Collaborator

rustbot commented May 23, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 23, 2025
@rust-log-analyzer

This comment has been minimized.

@yotamofek yotamofek force-pushed the pr/library/from-iter-char-string branch from 4e9a021 to 8365332 Compare May 23, 2025 13:51
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes complete sense to me! This will need T-libs-api approval though...

Implementation-wise, I think it'd be good to go through Vec's FromIterator implementation. That way we get to take advantage of all of its nice specialisations...

r? libs-api

Comment on lines 2380 to 2382
let mut buf = String::new();
buf.extend(iter);
buf
Copy link
Member

@joboet joboet May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something along the lines of

Suggested change
let mut buf = String::new();
buf.extend(iter);
buf
let mut buf = iter.into_iter().map(Char::to_u8).collect();
unsafe { String::from_utf8_unchecked(buf) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, that's clever, thanks!

Comment on lines 2390 to 2392
let mut buf = String::new();
buf.extend(iter);
buf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(likewise)

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 23, 2025
@rustbot rustbot assigned joshtriplett and unassigned joboet May 23, 2025
@yotamofek yotamofek force-pushed the pr/library/from-iter-char-string branch from 8365332 to 8569bb2 Compare May 23, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants