@@ -481,6 +481,22 @@ impl<T> Vec<T> {
481481 Self :: with_capacity_in ( capacity, Global )
482482 }
483483
484+ /// Constructs a new, empty `Vec<T>` with at least the specified capacity.
485+ ///
486+ /// The vector will be able to hold at least `capacity` elements without
487+ /// reallocating. This method is allowed to allocate for more elements than
488+ /// `capacity`. If `capacity` is 0, the vector will not allocate.
489+ ///
490+ /// # Errors
491+ ///
492+ /// Returns an error if the capacity exceeds `isize::MAX` _bytes_,
493+ /// or if the allocator reports allocation failure.
494+ #[ inline]
495+ #[ unstable( feature = "try_with_capacity" , issue = "91913" ) ]
496+ pub fn try_with_capacity ( capacity : usize ) -> Result < Self , TryReserveError > {
497+ Self :: try_with_capacity_in ( capacity, Global )
498+ }
499+
484500 /// Creates a `Vec<T>` directly from a pointer, a capacity, and a length.
485501 ///
486502 /// # Safety
@@ -672,6 +688,25 @@ impl<T, A: Allocator> Vec<T, A> {
672688 Vec { buf : RawVec :: with_capacity_in ( capacity, alloc) , len : 0 }
673689 }
674690
691+ /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
692+ /// with the provided allocator.
693+ ///
694+ /// The vector will be able to hold at least `capacity` elements without
695+ /// reallocating. This method is allowed to allocate for more elements than
696+ /// `capacity`. If `capacity` is 0, the vector will not allocate.
697+ ///
698+ /// # Errors
699+ ///
700+ /// Returns an error if the capacity exceeds `isize::MAX` _bytes_,
701+ /// or if the allocator reports allocation failure.
702+ #[ cfg( not( no_global_oom_handling) ) ]
703+ #[ inline]
704+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
705+ // #[unstable(feature = "try_with_capacity", issue = "91913")]
706+ pub fn try_with_capacity_in ( capacity : usize , alloc : A ) -> Result < Self , TryReserveError > {
707+ Ok ( Vec { buf : RawVec :: try_with_capacity_in ( capacity, alloc) ?, len : 0 } )
708+ }
709+
675710 /// Creates a `Vec<T, A>` directly from a pointer, a capacity, a length,
676711 /// and an allocator.
677712 ///
0 commit comments