Skip to content

Non-panicking version of Vec::remove #649

@BenjaminBrienen

Description

@BenjaminBrienen

Proposal

Problem statement

A non-panicking version of Vec::remove is useful for code that does not want the risk of panicking or where the possibility of panicking should be clear from reading the source code.

Motivating examples or use cases

This is what is currently necessary:
if index < my_vector.len() { Some(my_vector.remove(index)) } else { None }

And this still links to the panic handler. The remove function already does a length check and can handle that case gracefully.

Solution sketch

Return an option and unwrap it in the panicking implementation.

It could be said that this implies all panicking functions should have non-panicking variants. I don't see a problem with that "counter" argument. I think they can be added as needed to make the effort smaller.

Alternatives

try_remove could be a wrapper around remove, but that seems wrong

Links and related work

rust-lang/rust#77481

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions