@@ -2514,11 +2514,28 @@ impl<T, A: Allocator> Vec<T, A> {
2514
2514
/// ```
2515
2515
/// #![feature(push_mut)]
2516
2516
///
2517
- /// let mut vec = vec![];
2518
- /// // Due to current borrow checker limitations (see -Zpolonius), this is the only way to spell this right now.
2519
- /// let last = if let Some(v) = vec.last_mut() { v } else { vec.push_mut(0) };
2520
- /// *last += 6;
2521
- /// assert_eq!(vec, [6]);
2517
+ /// # #[allow(unused)]
2518
+ /// #[derive(PartialEq, Eq, Debug)]
2519
+ /// struct Item { identifier: &'static str, count: usize }
2520
+ ///
2521
+ /// impl Default for Item {
2522
+ /// fn default() -> Self {
2523
+ /// return Self { identifier: "stone", count: 64 }
2524
+ /// }
2525
+ /// }
2526
+ ///
2527
+ /// let mut items = vec![];
2528
+ ///
2529
+ /// // We can mutate the just-pushed value without having to fetch it again
2530
+ /// for count in [15, 35, 61] {
2531
+ /// let item = items.push_mut(Item::default());
2532
+ /// item.count = count;
2533
+ /// }
2534
+ ///
2535
+ /// assert_eq!(
2536
+ /// items,
2537
+ /// [Item { identifier: "stone", count: 15 }, Item { identifier: "stone", count: 35 }, Item { identifier: "stone", count: 61 }]
2538
+ /// );
2522
2539
/// ```
2523
2540
///
2524
2541
/// # Time complexity
@@ -2563,8 +2580,12 @@ impl<T, A: Allocator> Vec<T, A> {
2563
2580
///
2564
2581
/// Takes *O*(1) time.
2565
2582
#[ inline]
2583
+
2584
+ // Since there's currently no way to have multiple unstable attributes on the same item, I compromised.
2585
+ // Uncomment/delete the respective attribute when its respective issue stabilizes, since this falls under both.
2566
2586
#[ unstable( feature = "push_mut" , issue = "135974" ) ]
2567
2587
// #[unstable(feature = "vec_push_within_capacity", issue = "100486")]
2588
+
2568
2589
#[ must_use = "if you don't need a reference to the value, use Vec::push_within_capacity instead" ]
2569
2590
pub fn push_mut_within_capacity ( & mut self , value : T ) -> Result < & mut T , T > {
2570
2591
if self . len == self . buf . capacity ( ) {
0 commit comments