Skip to content

Peeking usize::MAX with peek_nth #1067

@Takashiidobe

Description

@Takashiidobe

Running this playground, it looks like you can't peek_nth with an argument of usize::MAX without panicking.

In a way, this makes sense cause peek_nth gets you an item with index in 0..usize::MAX, so getting the index usize::MAX itself is out of bounds. However, if you peek_nth past the end, this implementation returns None. I assume the implicit contract is that it should always return an option, and not panic.

It's pretty straighforward to special case usize::MAX and just return None if its provided as an argument to peek_nth. Otherwise, it can be kept as panicking and noted in the docs as an example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions