Skip to content

Tracking Issue for Vec::push_mut #135974

@balt-dev

Description

@balt-dev

Feature gate: #![feature(push_mut)]

This is a tracking issue for Vec::push_mut and similar methods, as discussed in the comments of this ACP. This adds a way to get a reference to the just-pushed value, which can eliminate having to .unwrap() or access the back of the list twice.

Public API

// All are `#[must_use]` to suggest using `.push(...)` if you don't need the reference

impl<T> Vec<T> {
    #[must_use]
    pub fn push_mut(&mut self, value: T) -> &mut T;
    #[must_use]
    pub fn insert_mut(&mut self, index: usize, element: T) -> &mut T;
    #[must_use]
    pub fn push_mut_within_capacity(&mut self, value: T) -> Result<&mut T, T>;
}

impl<T> VecDeque<T> {
    #[must_use]
    pub fn push_front_mut(&mut self, value: T) -> &mut T;
    #[must_use]
    pub fn push_back_mut(&mut self, value: T) -> &mut T;
    #[must_use]
    pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T;
}

impl<T> LinkedList<T> {
    #[must_use]
    pub fn push_front_mut(&mut self, elt: T) -> &mut T;
    #[must_use]
    pub fn push_back_mut(&mut self, elt: T) -> &mut T
}

Steps / History

Unresolved Questions

  • Does must_use make sense? Are there downsides of push_mut vs. push?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions