Skip to content
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

rename str::slice to str::slice_bytes #5793

Closed
erickt opened this issue Apr 9, 2013 · 5 comments
Closed

rename str::slice to str::slice_bytes #5793

erickt opened this issue Apr 9, 2013 · 5 comments
Labels
A-unicode Area: Unicode

Comments

@erickt
Copy link
Contributor

erickt commented Apr 9, 2013

str::slice works on byte indices, so it would make more sense if the function was called str::slice_bytes.

@Kimundi
Copy link
Member

Kimundi commented Apr 9, 2013

I think it should stay as is, a str is just a transmuted [u8] with the additional invariant that it contains utf8, so it's consistent. Furthermore it would make the functions that actually allow to slice as a &[u8] more confusing.

@metajack
Copy link
Contributor

nominating backwards compat

@bblum
Copy link
Contributor

bblum commented Aug 5, 2013

Can the function be used to cut apart unicode characters? I doubt the usefulness of such a function.

Whatever the answer, str::slice should be the name of a function that takes char indices, not byte indices.

@bluss
Copy link
Member

bluss commented Aug 5, 2013

str::slice has asserts to check that you don't slice in the middle of encoded codepoints. With that context, the slice_bytes name is not that great, because you have to slice it out as a valid string, not an arbitrary byte slice.

For working with strings, you either have to use byte indices or use character iterators, otherwise you run into the issues that come with char-based indexing being O(N).

@graydon
Copy link
Contributor

graydon commented Aug 15, 2013

This is working as intended. Slicing in the middle of a char is the same as slicing past the end of the array: it's a failure. We name the APIs with _char if they're character-based.

@graydon graydon closed this as completed Aug 15, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 17, 2020
improve advice in iter_nth_zero

fixes rust-lang#5783

*Please keep the line below*
changelog:  For iter_nth_zero, the "use .next()" replacement advice is on the last line of the code snippet, where it is vulnerable to truncation. Display that advice at the beginning instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-unicode Area: Unicode
Projects
None yet
Development

No branches or pull requests

6 participants