Skip to content

Tracking Issue for Vec::push_mut #135974

Open
@balt-dev

Description

@balt-dev

Feature gate: #![feature(push_mut)]

This is a tracking issue for Vec::push_mut, 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

#[must_use = "if you don't need a reference to the value, use Vec::push")]
pub fn push_mut(&mut self, value: T) -> &mut T { /* ... */ }

Steps / History

  • Final comment period (FCP)
    Stabilization PR

Unresolved Questions

None

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Jan 24, 2025
changed the title [-]Tracking Issue for push_mut[/-] [+]Tracking Issue for `Vec::push_mut`[/+] on Jan 24, 2025
balt-dev

balt-dev commented on Jan 24, 2025

@balt-dev
Author

Here's an example from the documentation:

#![feature(push_mut)]

let mut vec = vec![];
// Polonius moment.
let last = if let Some(v) = vec.last_mut() { v } else { vec.push_mut(0) };
*last += 6;
assert_eq!(vec, [6]);
balt-dev

balt-dev commented on Feb 18, 2025

@balt-dev
Author

(Note: I did go back and change the // Polonius moment. comment to something more descriptive.)

balt-dev

balt-dev commented on Jun 9, 2025

@balt-dev
Author

Thoughts on an unsafe push_raw that returns a *mut T?
Actually, wait, that's unnecessary since you can just downgrade the reference to a pointer in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Tracking Issue for `Vec::push_mut` · Issue #135974 · rust-lang/rust