-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Request: A way to collect a String
from an impl Iterator<item=u8>
#65538
Comments
I'm also not sure if this is possible - you would need to implement FromIterator for Result<String, Utf8Error> or so, right? That seems like it would overlap with the general impl for Result. |
Adding some context (from community discord), this is about avoiding the extra allocation when going from bytes to String, not necessarily going through |
It doesn't need to be usable with collect. It can be a method on the String type. let s: String= String::from_lossy_bytes_iterator(it);
let s: Result<String,???> = String::from_bytes_iterator(it2); That error case in the non-lossy version is going to be annoying to design. You could probably work it out to a normal UTF8Error by converting the String that did parse back into a Vec and then extending it with the remains of the iterator. |
I am confused. What extra allocation do you want to avoid? The case of non-utf 8 data? |
Supposing that you currently have a value with type As far as I can tell, there's no way to do this in core/alloc/std without first collecting into |
There doesn't even appear to be |
With https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8 that's not a separate allocation, though? |
It is in the lossy case. but closed in favor of #64727 since a solution there would solve here too. |
As much as rust seems to love iterators and also utf8, there doesn't seem to be a way to collect a
String
from an iterator of bytes. Everything forces you to collect into aVec<u8>
and then re-parse that into aString
.There is a
FromIterator<char>
, but there also doesn't appear to be a way to get achar
from 1 to 4 bytes, or any iterator adapter that will turn a stream of utf8 bytes into a stream ofchar
values.The text was updated successfully, but these errors were encountered: