-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling .tail()
on a empty slice panics
#24141
Comments
It seems like it should either return an empty slice, or just an Option ? |
imho either an |
Array access out of bound generally panics. Doesn't mean the docs can't be clearer, though. I thought we discussed Option, but I can't remember. |
Also, I find it weird to have |
I agree with @vberger. It seems logical that |
I will PR this tonight, does it require an RFC? On Tue, Apr 7, 2015 at 12:41 PM, Guillaume Gomez notifications@github.com
|
@richo: I was going to do it when someone would have give the start. Too bad for me... |
If you want to do it, feel free! On Tue, Apr 7, 2015 at 1:28 PM, Guillaume Gomez notifications@github.com
|
@richo: Well then I do it ! Thanks ^^ |
Still needs decision on RFC though, @steveklabnik ? |
Probably doesn't need a full RFC, but not guaranteed to be merged. |
Given that this is an unstable interface, I would assume changing it is just fine. But given the recent events today, I'm not exactly jumping at stamping non-rfc breaking changes, know what I mean? The PR will be small and not likely to rot, so I don't think it'd be harmful to send it regardless, though. |
Ok, thanks for your confirmation @steveklabnik. I'm "fixing" that then ! |
Strangely enough, there is no So |
It's only confusing if you're coming from a language like Haskell that defines I wonder if maybe we should actually move to something like fn pop_first<'a>(&'a self) -> Option<&'a self::Item, &'a [Self::Item]>
fn pop_last<'a>(&'a self) -> Option<&'a self::Item, &'a [Self::Item]> These would replace I'm suggesting this approach both because it fixes the naming, and because I'd expect that code that uses |
We can note that there is already
(that is what I was actually using before replacing my |
Copying my thoughts from #24184: Hmm, I have mixed reactions. I admire your attempt to find something categorically better than
(Also, in collections, we tend to use the names "front" and "back", so maybe we should stick to that, rather than "first/last". Or does that seem specific to |
When I'm coding, I consider it this way: |
Indeed returning Options everywhere can be quite a heavy, but in this case it would be appropriate to document when a function can panic. However, it raises an other point: where should it be documented in this case ? Documenting it on the trait would imply that every implementation is expected to panic, and I'm not sure we can document an implementation in a way that makes it well visible in the docs. |
Maybe off-topic, I missed the day when |
|
Forget about it, it turns out that in general people can hardly guarantee if a method can panic in some condition. |
To the best of my knowledge, our consensus some time ago with respect to slicing methods was that index syntax with ranges is the main way of creating a slice, and all methods will panic if range endpoints are out of bounds. We definitely realized, however, that there was a large use case for methods returning If we were to provide With respect to @kballard's proposal I agree with @nikomatsakis that it's a pretty nice signature to have, but the naming may need some work. @vberger raises a good point that I personally like @kballard's PR (#24184) but would want to figure out a better name (or at least convention for the name) beforehand. |
I chose "pop" because I wanted the connotation of "and return the removed element", but I agree that it's potentially misleading as "pop" usually mutates the receiver. I'd be fine with renaming these to
I agree, I think it's mostly the 3 of the 4 uses of
That's an important point. Personally, I think we aren't violating it; calling At the moment, I'm inclined to change the name to |
@kballard it looks like there's actually a bit of design space here that we may want to explore in a broader forum, would be interested in writing an RFC for these two functions? |
Ok, I'll try to do that this evening. |
Thanks! |
RFC has been submitted as rust-lang/rfcs#1058 |
Outdated because rust-lang/rfcs#1058 was accepted, and implemented in #26966, deprecating |
👍 |
Calling
.tail()
on a slice does no check at all (see https://github.com/rust-lang/rust/blob/master/src/libcore/slice.rs#L210 ), and will panic if called on a empty slice.Nothing suggests it in the documentation, I personally expected that calling
.tail()
on an empty slice would return an empty slice, as.first()
returns an Option.The text was updated successfully, but these errors were encountered: