-
Notifications
You must be signed in to change notification settings - Fork 19
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
Implement into_chars
for String
#268
Comments
We discussed this in the libs-api meeting yesterday and couldn't think of any other way to achieve this functionality. As such it's fine to add this as an unstable API. |
@Amanieu I don't find this method on the codebase, it is still valid and I may spend some time to write an impl? Didn't see referred PR also. |
Found this related PR - rust-lang/rust#50845 But it's even 5 years earlier than this issue. Let me see if we still want to add such methods. |
Seems that to avoid duplicate code (of Chars internal) and avoid always make a Chars to access those ability, we'd factor out the Chars functionality into a shared structure and perhaps extend See https://github.com/rust-lang/rust/pull/50845/files#r189071758 with @shepmaster and @nagisa . I may spend some time to try to make a draft in weeks. |
maybe use something like: pub struct IntoChars {
bytes: vec::IntoIter<u8>,
}
impl Iterator for IntoChars {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
let mut chars = unsafe { str::from_utf8_unchecked(self.bytes.as_slice()) }.char_indexes();
let retval = chars.next()?;
self.bytes.advance_by(chars.offset());
Some(retval)
}
...
}
... |
@programmerjake Thanks for sharing the snippet. This would create a wrapper struct (CharIndices) each time, but it can be cheap? I'll start with this direction then. |
Proposal
Problem statement
Currently there is no owning analog to the chars iterator. In most contexts this has no real effect, as
char
isCopy
, but it disables some cases where one wants to pass ownership of the underlyingString
along with the iterator.Motivating example
Implementing the following function requires some gymnastics.
Solution sketch
If
into_chars
existed, the above function would be simple to write.Alternatives
It is relatively straightforward to write an owning chars iterator outside of
std
. As a struct:As a closure:
Links and related work
Discussion on irlo.
The text was updated successfully, but these errors were encountered: