-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: str::chunks
with chunks being &str
#592
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
Comments
str::chunks
with chunks being &str
str::chunks
with chunks being &str
Why is a consistent number of USVs a useful operation to do? To me this seems like something for |
I agree that "number of characters" is generally not a desired operation, and that it's much better to defer to the various Unicode segmentation algorithms instead. I would argue that most of the issues here are the absence of methods like |
If you wanted to chunk over chars, you could do |
I'm ok to open a PR for Tho, I think that wrapping some text to fit a specific format can also be a common usage
=>
I think that only the first one is about ASCII |
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Problem statement
The std currently provides various methods for chunking slices (
array_chunks
) and iterators (chunks
,chunks_exact
,rchunks
,array_chunks
,utf8_chunks
, ...).However, there is no equivalent method for string slices. And currently, the "developer experience" related to chunks in
&str
can be improved.Motivating examples or use cases
Chunking is an action that may often be needed when working with data that can be seen as an iterator.
This is why there are methods for this with slices and iterators.
But, there are none for
&str
even tho it can be useful a lot of time!Here are some examples:
Currently we would do
hello---only----8-------chars---
Overall, everything that is about handling data with repetitive pattern or with some wrapping or formatting would benefit from this function.
Another problem is that,
array_chunks
doesn't have the same behaviour asslice::chunk
since the last element is discarded if it doesn't do the same size aschunk_size
which isn't always wanted.But, if you want to achieve the same thing in the current context, you will have create an unecessary vector:
It's
Also,
str::chunks()
is faster thanChars::array_chunks()
(without even consideringstr::from_utf8().unwrap()
)Solution sketch
str::Chunks
incore/src/str/iter.rs
and implementIterator
&DoubleEndedIterator
on itstr
:Implementation at https://github.com/tkr-sh/rust/tree/str-chunks
Drawbacks
.chunks()
on&str
isn't necessary clear if it's onu8
orchar
. Tho, if chunks are&str
it makes sens that it's onchar
s.Alternatives
.chars().collect()
thenvec.as_slice().chunks()
but it's significantly longer and is owning data that could be avoided. See motivation..chars().array_chunks()
but it's unstable, slower and doesn't behave in the same way. See motivation.Links and related work
slice::chunks(usize)
str::chars()
Iterator::array_chunks(usize)
str::chunks
,str::chunks_exact
, andstr::windows
#590It was rejected in part because
which shouldn't affect this ACP.
From rust-lang/rfcs#3818
The text was updated successfully, but these errors were encountered: