@@ -472,6 +472,48 @@ impl<T> Vec<T> {
472472 Self :: with_capacity_in ( capacity, Global )
473473 }
474474
475+ /// Tries to construct a new, empty `Vec<T>` with the specified capacity.
476+ ///
477+ /// The vector will be able to hold exactly `capacity` elements without
478+ /// reallocating. If `capacity` is 0, the vector will not allocate.
479+ ///
480+ /// It is important to note that although the returned vector has the
481+ /// *capacity* specified, the vector will have a zero *length*. For an
482+ /// explanation of the difference between length and capacity, see
483+ /// *[Capacity and reallocation]*.
484+ ///
485+ /// [Capacity and reallocation]: #capacity-and-reallocation
486+ ///
487+ /// # Examples
488+ ///
489+ /// ```
490+ /// let mut vec = Vec::try_with_capacity(10).unwrap();
491+ ///
492+ /// // The vector contains no items, even though it has capacity for more
493+ /// assert_eq!(vec.len(), 0);
494+ /// assert_eq!(vec.capacity(), 10);
495+ ///
496+ /// // These are all done without reallocating...
497+ /// for i in 0..10 {
498+ /// vec.push(i);
499+ /// }
500+ /// assert_eq!(vec.len(), 10);
501+ /// assert_eq!(vec.capacity(), 10);
502+ ///
503+ /// // ...but this may make the vector reallocate
504+ /// vec.push(11);
505+ /// assert_eq!(vec.len(), 11);
506+ /// assert!(vec.capacity() >= 11);
507+ ///
508+ /// let mut result = Vec::try_with_capacity(usize::MAX);
509+ /// assert!(result.is_err());
510+ /// ```
511+ #[ inline]
512+ #[ stable( feature = "kernel" , since = "1.0.0" ) ]
513+ pub fn try_with_capacity ( capacity : usize ) -> Result < Self , TryReserveError > {
514+ Self :: try_with_capacity_in ( capacity, Global )
515+ }
516+
475517 /// Creates a `Vec<T>` directly from the raw components of another vector.
476518 ///
477519 /// # Safety
@@ -617,6 +659,53 @@ impl<T, A: Allocator> Vec<T, A> {
617659 Vec { buf : RawVec :: with_capacity_in ( capacity, alloc) , len : 0 }
618660 }
619661
662+ /// Tries to construct a new, empty `Vec<T, A>` with the specified capacity
663+ /// with the provided allocator.
664+ ///
665+ /// The vector will be able to hold exactly `capacity` elements without
666+ /// reallocating. If `capacity` is 0, the vector will not allocate.
667+ ///
668+ /// It is important to note that although the returned vector has the
669+ /// *capacity* specified, the vector will have a zero *length*. For an
670+ /// explanation of the difference between length and capacity, see
671+ /// *[Capacity and reallocation]*.
672+ ///
673+ /// [Capacity and reallocation]: #capacity-and-reallocation
674+ ///
675+ /// # Examples
676+ ///
677+ /// ```
678+ /// #![feature(allocator_api)]
679+ ///
680+ /// use std::alloc::System;
681+ ///
682+ /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
683+ ///
684+ /// // The vector contains no items, even though it has capacity for more
685+ /// assert_eq!(vec.len(), 0);
686+ /// assert_eq!(vec.capacity(), 10);
687+ ///
688+ /// // These are all done without reallocating...
689+ /// for i in 0..10 {
690+ /// vec.push(i);
691+ /// }
692+ /// assert_eq!(vec.len(), 10);
693+ /// assert_eq!(vec.capacity(), 10);
694+ ///
695+ /// // ...but this may make the vector reallocate
696+ /// vec.push(11);
697+ /// assert_eq!(vec.len(), 11);
698+ /// assert!(vec.capacity() >= 11);
699+ ///
700+ /// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
701+ /// assert!(result.is_err());
702+ /// ```
703+ #[ inline]
704+ #[ stable( feature = "kernel" , since = "1.0.0" ) ]
705+ pub fn try_with_capacity_in ( capacity : usize , alloc : A ) -> Result < Self , TryReserveError > {
706+ Ok ( Vec { buf : RawVec :: try_with_capacity_in ( capacity, alloc) ?, len : 0 } )
707+ }
708+
620709 /// Creates a `Vec<T, A>` directly from the raw components of another vector.
621710 ///
622711 /// # Safety
0 commit comments