-
Notifications
You must be signed in to change notification settings - Fork 20
Implement into_chars
for String
#268
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
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. |
Impl String::into_chars Tracking issue - rust-lang#133125 r? `@programmerjake` `@kennytm` `@Amanieu` This refers to rust-lang/libs-team#268 Before adding tests and creating a tracking issue, I'd like to reach a consensus on the implementation direction and two questions: 1. Whether we'd add a `String::into_char_indices` method also? 2. See inline comment.
Rollup merge of rust-lang#133057 - tisonkun:into-chars, r=Amanieu Impl String::into_chars Tracking issue - rust-lang#133125 r? `@programmerjake` `@kennytm` `@Amanieu` This refers to rust-lang/libs-team#268 Before adding tests and creating a tracking issue, I'd like to reach a consensus on the implementation direction and two questions: 1. Whether we'd add a `String::into_char_indices` method also? 2. See inline comment.
Impl String::into_chars Tracking issue - rust-lang#133125 r? `@programmerjake` `@kennytm` `@Amanieu` This refers to rust-lang/libs-team#268 Before adding tests and creating a tracking issue, I'd like to reach a consensus on the implementation direction and two questions: 1. Whether we'd add a `String::into_char_indices` method also? 2. See inline comment.
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: