|
5 | 5 |
|
6 | 6 | # Summary |
7 | 7 |
|
8 | | -Add some methods that already exist on slices to strings and vice versa. |
9 | | -Specifically, the following methods should be added: |
| 8 | +Add some methods that already exist on slices to strings. Specifically, the |
| 9 | +following methods should be added: |
10 | 10 |
|
11 | | -- `str::chunks` |
12 | | -- `str::windows` |
13 | 11 | - `str::into_string` |
14 | | -- `String::into_boxed_slice` |
15 | | -- `<[T]>::subslice_offset` |
| 12 | +- `String::into_boxed_str` |
16 | 13 |
|
17 | 14 | # Motivation |
18 | 15 |
|
19 | 16 | Conceptually, strings and slices are similar types. Many methods are already |
20 | 17 | shared between the two types due to their similarity. However, not all methods |
21 | 18 | are shared between the types, even though many could be. This is a little |
22 | 19 | unexpected and inconsistent. Because of that, this RFC proposes to remedy this |
23 | | -by adding a few methods to both strings and slices to even out these two types’ |
24 | | -available methods. |
| 20 | +by adding a few methods to strings to even out these two types’ available |
| 21 | +methods. |
25 | 22 |
|
26 | | -# Detailed design |
27 | | - |
28 | | -Add the following methods to `str`, presumably as inherent methods: |
| 23 | +Specifically, it is currently very difficult to construct a `Box<str>`, while it |
| 24 | +is fairly simple to make a `Box<[T]>` by using `Vec::into_boxed_slice`. This RFC |
| 25 | +proposes a means of creating a `Box<str>` by converting a `String`. |
29 | 26 |
|
30 | | -- `chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields the |
31 | | - *characters* (not bytes) of the string in groups of `n` at a time. Iterator |
32 | | - element type: `&str`. |
| 27 | +# Detailed design |
33 | 28 |
|
34 | | -- `windows(&self, n: usize) -> Windows`: Returns an iterator over all contiguous |
35 | | - windows of character length `n`. Iterator element type: `&str`. |
| 29 | +Add the following method to `str`, presumably as an inherent method: |
36 | 30 |
|
37 | 31 | - `into_string(self: Box<str>) -> String`: Returns `self` as a `String`. This is |
38 | 32 | equivalent to `[T]`’s `into_vec`. |
39 | 33 |
|
40 | | -`split_at(&self, mid: usize) -> (&str, &str)` would also be on this list, but |
41 | | -there is [an existing RFC](https://github.com/rust-lang/rfcs/pull/1123) for it. |
42 | | - |
43 | 34 | Add the following method to `String` as an inherent method: |
44 | 35 |
|
45 | | -- `into_boxed_slice(self) -> Box<str>`: Returns `self` as a `Box<str>`, |
| 36 | +- `into_boxed_str(self) -> Box<str>`: Returns `self` as a `Box<str>`, |
46 | 37 | reallocating to cut off any excess capacity if needed. This is required to |
47 | | - provide a safe means of creating `Box<str>`. |
48 | | - |
49 | | -Add the following method to `[T]` (for all `T`), presumably as an inherent |
50 | | -method: |
| 38 | + provide a safe means of creating `Box<str>`. This is equivalent to `Vec<T>`’s |
| 39 | + `into_boxed_slice`. |
51 | 40 |
|
52 | | -- `subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset (in |
53 | | - elements) of an inner slice relative to an outer slice. Panics of `inner` is |
54 | | - not contained within `self`. |
55 | 41 |
|
56 | 42 | # Drawbacks |
57 | 43 |
|
58 | | -- `str::subslice_offset` is already unstable, so creating a similar method on |
59 | | - `[T]` is perhaps not such a good idea. |
| 44 | +None, yet. |
60 | 45 |
|
61 | 46 | # Alternatives |
62 | 47 |
|
63 | | -- Do a subset of the proposal. For example, the `Box<str>`-related methods could |
64 | | - be removed. |
| 48 | +- The original version of this RFC had a few extra methods: |
| 49 | + - `str::chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields |
| 50 | + the *characters* (not bytes) of the string in groups of `n` at a time. |
| 51 | + Iterator element type: `&str`. |
| 52 | + |
| 53 | + - `str::windows(&self, n: usize) -> Windows`: Returns an iterator over all |
| 54 | + contiguous windows of character length `n`. Iterator element type: `&str`. |
| 55 | + |
| 56 | + This and `str::chunks` aren’t really useful without proper treatment of |
| 57 | + graphemes, so they were removed from the RFC. |
| 58 | + |
| 59 | + - `<[T]>::subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset |
| 60 | + (in elements) of an inner slice relative to an outer slice. Panics of |
| 61 | + `inner` is not contained within `self`. |
| 62 | + |
| 63 | + `str::subslice_offset` isn’t yet stable and its usefulness is dubious, so |
| 64 | + this method was removed from the RFC. |
| 65 | + |
65 | 66 |
|
66 | 67 | # Unresolved questions |
67 | 68 |
|
|
0 commit comments