Skip to content

Commit a0d4344

Browse files
committed
Update with libs team consensus
1 parent 41ae6e6 commit a0d4344

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

text/0000-slice-tail-redesign.md

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
# Summary
77

8-
Replace `slice.tail()`, `slice.init()` with new methods `slice.shift_first()`,
9-
`slice.shift_last()`.
8+
Replace `slice.tail()`, `slice.init()` with new methods `slice.split_first()`,
9+
`slice.split_last()`.
1010

1111
# Motivation
1212

@@ -20,10 +20,10 @@ remaining methods that panic without taking an explicit index.
2020
A conservative change here would be to simply change `head()`/`tail()` to return
2121
`Option`, but I believe we can do better. These operations are actually
2222
specializations of `split_at()` and should be replaced with methods that return
23-
`Option<(T,&[T])>`. This makes the common operation of processing the first/last
24-
element and the remainder of the list more ergonomic, with very low impact on
25-
code that only wants the remainder (such code only has to add `.1` to the
26-
expression). This has an even more significant effect on code that uses the
23+
`Option<(&T,&[T])>`. This makes the common operation of processing the
24+
first/last element and the remainder of the list more ergonomic, with very low
25+
impact on code that only wants the remainder (such code only has to add `.1` to
26+
the expression). This has an even more significant effect on code that uses the
2727
mutable variants.
2828

2929
# Detailed design
@@ -32,21 +32,21 @@ The methods `head()`, `tail()`, `head_mut()`, and `tail_mut()` will be removed,
3232
and new methods will be added:
3333

3434
```rust
35-
fn shift_first(&self) -> Option<(&T, &[T])>;
36-
fn shift_last(&self) -> Option<(&T, &[T])>;
37-
fn shift_first_mut(&mut self) -> Option<(&mut T, &mut [T])>;
38-
fn shift_last_mut(&mut self) -> Option<(&mut T, &mut [T])>;
35+
fn split_first(&self) -> Option<(&T, &[T])>;
36+
fn split_last(&self) -> Option<(&T, &[T])>;
37+
fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])>;
38+
fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])>;
3939
```
4040

4141
Existing code using `tail()` or `init()` could be translated as follows:
4242

4343
* `slice.tail()` becomes `&slice[1..]`
4444
* `slice.init()` becomes `&slice[..slice.len()-1]` or
45-
`slice.shift_last().unwrap().1`
45+
`slice.split_last().unwrap().1`
4646

4747
It is expected that a lot of code using `tail()` or `init()` is already either
4848
testing `len()` explicitly or using `first()` / `last()` and could be refactored
49-
to use `shift_first()` / `shift_last()` in a more ergonomic fashion. As an
49+
to use `split_first()` / `split_last()` in a more ergonomic fashion. As an
5050
example, the following code from typeck:
5151

5252
```rust
@@ -57,7 +57,7 @@ if variant.fields.len() > 0 {
5757
can be rewritten as:
5858

5959
```rust
60-
if let Some((_, init_fields)) = variant.fields.shift_last() {
60+
if let Some((_, init_fields)) = variant.fields.split_last() {
6161
for field in init_fields {
6262
```
6363

@@ -71,14 +71,14 @@ let args_ = args.tail();
7171
can be rewritten as:
7272

7373
```rust
74-
let (argv0, args_) = args.shift_first().unwrap();
74+
let (argv0, args_) = args.split_first().unwrap();
7575
```
7676

7777
(the `clone()` ended up being unnecessary).
7878

7979
# Drawbacks
8080

81-
The expression `slice.shift_last().unwrap().1` is more cumbersome than
81+
The expression `slice.split_last().unwrap().1` is more cumbersome than
8282
`slice.init()`. However, this is primarily due to the need for `.unwrap()`
8383
rather than the need for `.1`, and would affect the more conservative solution
8484
(of making the return type `Option<&[T]>`) as well. Furthermore, the more
@@ -95,17 +95,3 @@ function names should be (the current names are considered suboptimal).
9595
Just deprecate the current methods without adding replacements. This gets rid of
9696
the odd methods today, but it doesn't do anything to make it easier to safely
9797
perform these operations.
98-
99-
# Unresolved questions
100-
101-
Is the name correct? There's precedent in this name in the form of
102-
[`str::slice_shift_char()`][slice_shift_char]. An alternative name might be
103-
`pop_first()`/`pop_last()`, or `shift_front()`/`shift_back()` (although the
104-
usage of `first`/`last` was chosen to match the existing methods `first()` and
105-
`last()`). Another option is `split_first()`/`split_last()`.
106-
107-
Should `shift_last()` return `Option<(&T, &[T])>` or `Option<(&[T], &T)>`?
108-
I believe that the former is correct with this name, but the latter might be
109-
more suitable given the name `split_last()`.
110-
111-
[slice_shift_char]: http://doc.rust-lang.org/nightly/std/primitive.str.html#method.slice_shift_char

0 commit comments

Comments
 (0)