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 upRFC for replacing slice::tail()/init() with new methods #1058
Conversation
lilyball
referenced this pull request
Apr 12, 2015
Closed
Calling `.tail()` on a empty slice panics #24141
This comment has been minimized.
This comment has been minimized.
|
There is some existing discussion in rust-lang/rust#24141 and rust-lang/rust#24184 |
This comment has been minimized.
This comment has been minimized.
|
I really like the |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Apr 12, 2015
|
The basic idea is nice, but I personally don't like |
This comment has been minimized.
This comment has been minimized.
Florob
commented
Apr 13, 2015
|
I like the general approach. In terms of bike-shedding I think I'd prefer |
This comment has been minimized.
This comment has been minimized.
|
The basic idea is great! I prefer |
This comment has been minimized.
This comment has been minimized.
|
How do you feel about With the name |
This comment has been minimized.
This comment has been minimized.
|
I prefer I'm aware that the word "shift" appears in In that vein, I think users will be more familiar with In fact I'm all for renaming |
This comment has been minimized.
This comment has been minimized.
|
I feel |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
if there is something called if there is something called |
This comment has been minimized.
This comment has been minimized.
|
I see what you mean now. I agree. |
This comment has been minimized.
This comment has been minimized.
|
I like the idea and the return values. I like the single element being returned first in both cases. I have no preference as to shift vs split. |
This comment has been minimized.
This comment has been minimized.
|
assigning to @aturon (i.e. to shepherd). |
pnkfelix
assigned
aturon
Apr 16, 2015
nikomatsakis
referenced this pull request
Apr 27, 2015
Closed
Add checkup in tail function and return Option type #24172
alexcrichton
referenced this pull request
May 1, 2015
Closed
Replace slice methods tail(), init() with pop_first(), pop_last() #24184
aturon
added
the
T-libs
label
May 22, 2015
Gankro
referenced this pull request
Jun 4, 2015
Closed
RFC: Allow re-exporting associated items with `use` #1150
P1start
referenced this pull request
Jun 10, 2015
Merged
Add some of `[T]`’s methods to strings and vice versa #1152
aturon
removed their assignment
Jun 11, 2015
Gankro
self-assigned this
Jun 11, 2015
Gankro
reviewed
Jun 11, 2015
| The expression `slice.shift_last().unwrap.1` is more cumbersome than | ||
| `slice.init()`. However, this is primarily due to the need for `.unwrap()` | ||
| rather than the need for `.1`, and would affect the more conservative solution | ||
| (of making the return type `Option<&[T]>`) as well. |
This comment has been minimized.
This comment has been minimized.
Gankro
Jun 11, 2015
Contributor
This seems like a non-sequitur. The idiomatic translation is:
slice.tail()->&slice[1..]slice.init()->&slice[..slice.len() - 1]
No? Basically init/tail have really just been superceeded by the vastly more flexible slicing syntax (we don't even need to have _mut variants because of slicing, woo!)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
lilyball
Jul 1, 2015
Author
Contributor
&slice[..slice.len() - 1] only works if you have the slice stored in a variable. If it's the result of some expression, you need to introduce a temporary variable to do that. Which is why I used slice.shift_last().unwrap.1 instead.
This comment has been minimized.
This comment has been minimized.
lilyball
Jul 1, 2015
Author
Contributor
Also to note, the &slice[..slice.len()-1] alternative was given up above on line 45, this drawback is only calling out the awkwardness of the shift_last() approach.
Gankro
reviewed
Jun 11, 2015
|
|
||
| # Drawbacks | ||
|
|
||
| The expression `slice.shift_last().unwrap.1` is more cumbersome than |
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
Jun 11, 2015
|
|
||
| ```rust | ||
| if let Some((_, init_fields)) = variant.fields.shift_last() { | ||
| for field in init_fields { |
This comment has been minimized.
This comment has been minimized.
Gankro
Jun 11, 2015
Contributor
vs
let len = variant.fields.len();
if len > 0 {
for field in &variant.fields[.. len - 1] {vs
variant.fields.shift_last().map(|(_, init_fields)| {
for field in init_fields {(I find if-let to be really noisy, especially when working with options)
This comment has been minimized.
This comment has been minimized.
|
Misc thoughts:
I am not super sold on having methods for this overall, but I acknowledge that there's something intrinsically special about the first and last elements in lists. |
This comment has been minimized.
This comment has been minimized.
|
This RFC is now entering its week-long final comment period. |
alexcrichton
added
the
final-comment-period
label
Jul 1, 2015
This comment has been minimized.
This comment has been minimized.
jongiddy
commented
Jul 6, 2015
|
One reason to keep the single element as the first return value for |
This comment has been minimized.
This comment has been minimized.
jarcane
commented
Jul 6, 2015
|
Furthermore, I can't say as these alternatives seem particularly palatable:
Given the choice, I'd choose the former, though a more consistent set of either head/tail, or first/rest, would be a good idea. |
This comment has been minimized.
This comment has been minimized.
|
I'd be interested if we could get a bit of a survey of how these functions are used more broadly than just in the main distribution. That said, a quick glance at the github search results... turned up essentially nothing. (I saw one use of These are certainly useful for "triangular" iteration: fn foo(mut v: &mut [T]) {
while let Some((head, rest)) = v.split_first_mut() {
for x in rest { pairwise_mut(head, x) }
v = rest;
}
}(I'm in favour of |
This comment has been minimized.
This comment has been minimized.
|
The consensus of the libs team was to merge this RFC with the |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Updated. |
alexcrichton
referenced this pull request
Jul 8, 2015
Closed
Rename slice::{tail, init} per RFC 1058 #26906
alexcrichton
merged commit a0d4344
into
rust-lang:master
Jul 8, 2015
This comment has been minimized.
This comment has been minimized.
|
Thanks @kballard! |
lilyball commentedApr 12, 2015
Rendered.
A PR for this has already been submitted as rust-lang/rust#24184.