Skip to content

Missing docs for panics when slicing with ranges out of bounds #100190

Open
@Sejsel

Description

@Sejsel

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 indices 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-sliceArea: `[T]`E-help-wantedCall for participation: Help is requested to fix this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions