-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Potential documentation gap in impl Index<Range<usize>> for [T]
#121568
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
Comments
Implementors of the trait might or might not panic on third party types, so 'may' is technically correct. The documentation for the implementations themselves could say 'will' |
"may panic" is correct on the |
@rustbot claim This is a weird one. It is very difficult to find this specific implementation from the docs (having to jump through SliceIndex). We can add documentation for each |
Implementation note: The most probable place for users to find the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html On that page, documentation added to specific methods will not be visible. As such, I opted to add the comments to the impl blocks directly. Helps with rust-lang#121568.
Implementation note: The most probable place for users to find the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html On that page, documentation added to specific methods will not be visible. As such, I opted to add the comments to the impl blocks directly. Helps with rust-lang#121568.
…trieb doc: describe panic conditions for SliceIndex implementations Implementation note: The most probable place for users to find the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html On that page, documentation added to specific methods will not be visible. As such, I opted to add the comments to the impl blocks directly. Helps with rust-lang#121568.
Rollup merge of rust-lang#123271 - JaniM:janim/sliceindex-doc, r=Nilstrieb doc: describe panic conditions for SliceIndex implementations Implementation note: The most probable place for users to find the documentation is at https://doc.rust-lang.org/std/slice/trait.SliceIndex.html On that page, documentation added to specific methods will not be visible. As such, I opted to add the comments to the impl blocks directly. Helps with rust-lang#121568.
Location
Index::index
Summary
In Rust, if you attempt to index a slice or array with a "backwards"
Range<usize>
, the program panics:This behavior is unsurprising, but not obvious: a reasonable alternative behavior would be to just return the empty slice
&[]
. This is what python does.The documentation for Index::index states that the indexing operation "May panic if the index is out of bounds", but doesn't define what it means to be "out of bounds". I'd argue that in this example, the Range
4..1
is actually not out-of-bounds for an array of size 7 because both 4 and 1 are less than 7 and therefore, by the letter of the documentation, the panic behavior here is wrong and returning the empty slice&[]
is a more reasonable behavior to expect than panicking.I feel like the documentation should make explicitly clear that this panics if either of the following are true:
range.end > slice.len()
range.end < range.start
Also the documentation should make clear that this does panic, not just that it might, because surely this panic is not UB.
The text was updated successfully, but these errors were encountered: