Skip to content

Vec::insert should allow pushing onto the end #21900

@leifwalsh

Description

@leifwalsh

Consider this pattern:

fn find_index(a: &[i32], extra_info: T) -> Option<usize> {
    // ...
    Some(4)  // or whatever
}

fn foo() {
    let mut v = Vec::new();
    loop {
        match find_index(&v[], some_stuff) {
            Some(i) => v.insert(i, other_stuff),
            None => break,
        }
    }
}

In rust, this panics if i == v.len(), meaning that in that example we'd have to explicitly check for that case and use v.push(other_stuff) instead, while I think in many languages, the vector/array/list's insert method allows that case and implements it as identical to Vec::push.

I'd like to propose that rust's Vec do that too, and I'd be happy to write a PR if someone can point me toward the code and tests that need to be changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions