rust icon indicating copy to clipboard operation
rust copied to clipboard

Missing docs for panics when slicing with ranges out of bounds

Open Sejsel opened this issue 3 years ago • 0 comments

Location

Somewhere near slices and ranges, maybe in sliceable types as well.

Summary

Currently there doesn't seem to be any documentation on slices (both std::slice and the primitive type) or ranges for their panic behavior when range indexes are out of range (unless I missed it somewhere).

This notably means I cannot find any documentation for the following behavior (playground):

fn main() {
    let vec = vec![1, 2, 3];
    println!("{:?}", &vec[2..]); // [3]
    println!("{:?}", &vec[3..]); // []
    println!("{:?}", &vec[4..]); // panic!
}

This behavior is rather non-obvious (although convenient for my current use case), I had expected the second slice to panic or the third one to also return [] for consistency. Either way, changing that would be a breaking change, so I would at least expect it to be documented somewhere.

Sejsel avatar Aug 06 '22 08:08 Sejsel