-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC for replacing slice::tail()/init() with new methods #1058
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
There is some existing discussion in rust-lang/rust#24141 and rust-lang/rust#24184 |
I really like the |
The basic idea is nice, but I personally don't like |
I like the general approach. In terms of bike-shedding I think I'd prefer |
The basic idea is great! I prefer |
How do you feel about With the name |
Add the suggested `split_` prefix as a name option, albeit with the names `split_first()`/`split_last()` instead of the originally-suggested `split_init()`/`split_tail()`. Add a question about the return type of `shift_last()`.
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 |
I feel |
if there is something called if there is something called |
I see what you mean now. I agree. |
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. |
assigning to @aturon (i.e. to shepherd). |
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. |
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.
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!)
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.
Couldn't you potentially typo and write &slice[..slice.len()]
?
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.
&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.
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.
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.
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 RFC is now entering its week-long final comment period. |
One reason to keep the single element as the first return value for |
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. |
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 |
The consensus of the libs team was to merge this RFC with the |
@alexcrichton Updated. |
Thanks @kballard! |
Rendered.
A PR for this has already been submitted as rust-lang/rust#24184.