Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd some of `[T]`’s methods to strings and vice versa #1152
Conversation
alexcrichton
added
the
T-libs
label
Jun 6, 2015
This comment has been minimized.
This comment has been minimized.
|
Is the reason for adding |
This comment has been minimized.
This comment has been minimized.
|
@Kimundi I’ve wanted to use it in the past, but yeah, it’s mainly just to match the |
This comment has been minimized.
This comment has been minimized.
|
Is there really a usecase for str::chunks or windows? |
This comment has been minimized.
This comment has been minimized.
gkoz
commented
Jun 6, 2015
|
Out of curiosity, what is |
This comment has been minimized.
This comment has been minimized.
|
@gkoz In principle it's just a runtime-constructed |
This comment has been minimized.
This comment has been minimized.
|
@Gankro |
This comment has been minimized.
This comment has been minimized.
|
Hm, I suspect |
SimonSapin
reviewed
Jun 9, 2015
| Add the following methods to `str`, presumably as inherent methods: | ||
|
|
||
| - `chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields the | ||
| *characters* (not bytes) of the string in groups of `n` at a time. Iterator |
This comment has been minimized.
This comment has been minimized.
SimonSapin
Jun 9, 2015
Contributor
“Character” is kinda ambiguous. (Unicode has four different definitions.) Maybe use chars instead?
This comment has been minimized.
This comment has been minimized.
|
+1 to functions involving -1 to |
This comment has been minimized.
This comment has been minimized.
|
I just stumbled upon the lack of |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
comex
commented
Jun 11, 2015
|
May be out of scope, but I'd like to see some form of |
Gankro
self-assigned this
Jun 11, 2015
This comment has been minimized.
This comment has been minimized.
|
I concur with @huonw on the dubiousness of windows and chunks in the face of combining characters. It's not clear to me how the Sørensen–Dice coefficient is even supposed to interact with utf8 in that regard. I'm completely indifferent on subslice_offset |
alexcrichton
added
the
final-comment-period
label
Jun 16, 2015
This comment has been minimized.
This comment has been minimized.
|
This RFC is now entering its final comment period. |
alexcrichton
reviewed
Jun 16, 2015
|
|
||
| - `subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset (in | ||
| elements) of an inner slice relative to an outer slice. Panics of `inner` is | ||
| not contained within `self`. |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 16, 2015
Member
I would personally prefer to not add this method to slices just yet, I suspect it won't survive stabilization of strings.
alexcrichton
reviewed
Jun 16, 2015
|
|
||
| Add the following method to `String` as an inherent method: | ||
|
|
||
| - `into_boxed_slice(self) -> Box<str>`: Returns `self` as a `Box<str>`, |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 16, 2015
Member
Perhaps this could be into_boxed_str? I think we tend to refer to string slices as str instead of slice.
This comment has been minimized.
This comment has been minimized.
nagisa
Jun 17, 2015
Contributor
Perhaps this could be an implementation of Into trait? I don’t see this being used enough to warrant a new method. I’d actually argue for deprecation of Vec::into_boxed_slice and implementation of Into trait too.
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 17, 2015
Member
That's somewhat related to #1153, but relying solely on Into unfortunately isn't without downsides. For example if I have a String and I know that I want to go to a Box<str> using the .into() method would require a type annotation, where a method like this would not. Overall we've been thinking so far that specific conversions (e.g. like this) will have named methods so they can be called ergonomically, but there will also likely be an Into implementation to satisfy generic bounds as well. Now that being said, we haven't yet posted the RFC to clarify these conversion conventions.
This comment has been minimized.
This comment has been minimized.
|
I also agree with @huonw about being hesitant to add windows/chunks to strings due to unicode fun stuff. |
nagisa
reviewed
Jun 17, 2015
| - `windows(&self, n: usize) -> Windows`: Returns an iterator over all contiguous | ||
| windows of character length `n`. Iterator element type: `&str`. | ||
|
|
||
| - `into_string(self: Box<str>) -> String`: Returns `self` as a `String`. This is |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The |
This comment has been minimized.
This comment has been minimized.
|
@P1start the libs team seems pretty unanimous about |
This comment has been minimized.
This comment has been minimized.
|
@Gankro Yep, that’s fine by me. I’ll update the RFC soon to include just those methods (and rename |
This comment has been minimized.
This comment has been minimized.
|
RFC updated, removing |
alexcrichton
referenced this pull request
Jul 1, 2015
Closed
Add conversions from Box<str> and String #26697
This comment has been minimized.
This comment has been minimized.
|
The consensus of the libs team is to merge this RFC, so I will do so. Thanks again @P1start! |
P1start commentedJun 6, 2015
Add some methods that already exist on slices to strings and vice versa. Specifically, the following methods should be added:
str::chunksstr::windowsstr::into_stringString::into_boxed_slice<[T]>::subslice_offsetRendered