|
| 1 | +- Feature Name: `slice_string_symmetry` |
| 2 | +- Start Date: 2015-06-06 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Add some methods that already exist on slices to strings and vice versa. |
| 9 | +Specifically, the following methods should be added: |
| 10 | + |
| 11 | +- `str::chunks` |
| 12 | +- `str::windows` |
| 13 | +- `str::into_string` |
| 14 | +- `String::into_boxed_slice` |
| 15 | +- `<[T]>::subslice_offset` |
| 16 | + |
| 17 | +# Motivation |
| 18 | + |
| 19 | +Conceptually, strings and slices are similar types. Many methods are already |
| 20 | +shared between the two types due to their similarity. However, not all methods |
| 21 | +are shared between the types, even though many could be. This is a little |
| 22 | +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. |
| 25 | + |
| 26 | +# Detailed design |
| 27 | + |
| 28 | +Add the following methods to `str`, presumably as inherent methods: |
| 29 | + |
| 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`. |
| 33 | + |
| 34 | +- `windows(&self, n: usize) -> Windows`: Returns an iterator over all contiguous |
| 35 | + windows of character length `n`. Iterator element type: `&str`. |
| 36 | + |
| 37 | +- `into_string(self: Box<str>) -> String`: Returns `self` as a `String`. This is |
| 38 | + equivalent to `[T]`’s `into_vec`. |
| 39 | + |
| 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 | +Add the following method to `String` as an inherent method: |
| 44 | + |
| 45 | +- `into_boxed_slice(self) -> Box<str>`: Returns `self` as a `Box<str>`, |
| 46 | + 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: |
| 51 | + |
| 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 | + |
| 56 | +# Drawbacks |
| 57 | + |
| 58 | +- `str::subslice_offset` is already unstable, so creating a similar method on |
| 59 | + `[T]` is perhaps not such a good idea. |
| 60 | + |
| 61 | +# Alternatives |
| 62 | + |
| 63 | +- Do a subset of the proposal. For example, the `Box<str>`-related methods could |
| 64 | + be removed. |
| 65 | + |
| 66 | +# Unresolved questions |
| 67 | + |
| 68 | +None. |
0 commit comments