-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add some of [T]
’s methods to strings and vice versa
#1152
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
Merged
alexcrichton
merged 2 commits into
rust-lang:master
from
ftxqxd:string-slice-api-symmetry
Jul 1, 2015
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
- Feature Name: `slice_string_symmetry` | ||
- Start Date: 2015-06-06 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add some methods that already exist on slices to strings. Specifically, the | ||
following methods should be added: | ||
|
||
- `str::into_string` | ||
- `String::into_boxed_str` | ||
|
||
# Motivation | ||
|
||
Conceptually, strings and slices are similar types. Many methods are already | ||
shared between the two types due to their similarity. However, not all methods | ||
are shared between the types, even though many could be. This is a little | ||
unexpected and inconsistent. Because of that, this RFC proposes to remedy this | ||
by adding a few methods to strings to even out these two types’ available | ||
methods. | ||
|
||
Specifically, it is currently very difficult to construct a `Box<str>`, while it | ||
is fairly simple to make a `Box<[T]>` by using `Vec::into_boxed_slice`. This RFC | ||
proposes a means of creating a `Box<str>` by converting a `String`. | ||
|
||
# Detailed design | ||
|
||
Add the following method to `str`, presumably as an inherent method: | ||
|
||
- `into_string(self: Box<str>) -> String`: Returns `self` as a `String`. This is | ||
equivalent to `[T]`’s `into_vec`. | ||
|
||
Add the following method to `String` as an inherent method: | ||
|
||
- `into_boxed_str(self) -> Box<str>`: Returns `self` as a `Box<str>`, | ||
reallocating to cut off any excess capacity if needed. This is required to | ||
provide a safe means of creating `Box<str>`. This is equivalent to `Vec<T>`’s | ||
`into_boxed_slice`. | ||
|
||
|
||
# Drawbacks | ||
|
||
None, yet. | ||
|
||
# Alternatives | ||
|
||
- The original version of this RFC had a few extra methods: | ||
- `str::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 element type: `&str`. | ||
|
||
- `str::windows(&self, n: usize) -> Windows`: Returns an iterator over all | ||
contiguous windows of character length `n`. Iterator element type: `&str`. | ||
|
||
This and `str::chunks` aren’t really useful without proper treatment of | ||
graphemes, so they were removed from the RFC. | ||
|
||
- `<[T]>::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`. | ||
|
||
`str::subslice_offset` isn’t yet stable and its usefulness is dubious, so | ||
this method was removed from the RFC. | ||
|
||
|
||
# Unresolved questions | ||
|
||
None. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/rust-lang/rfcs/pull/1152/files#r32605082 ditto.