Skip to content

Commit a533504

Browse files
committed
Add try_reserve to HashSet
1 parent 185ed98 commit a533504

File tree

1 file changed

+24
-0
lines changed
  • src/libstd/collections/hash

1 file changed

+24
-0
lines changed

src/libstd/collections/hash/set.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::borrow::Borrow;
2+
use crate::collections::CollectionAllocErr;
23
use crate::fmt;
34
use crate::hash::{Hash, BuildHasher};
45
use crate::iter::{Chain, FromIterator, FusedIterator};
@@ -357,6 +358,29 @@ impl<T, S> HashSet<T, S>
357358
self.map.reserve(additional)
358359
}
359360

361+
/// Tries to reserve capacity for at least `additional` more elements to be inserted
362+
/// in the given `HashSet<K,V>`. The collection may reserve more space to avoid
363+
/// frequent reallocations.
364+
///
365+
/// # Errors
366+
///
367+
/// If the capacity overflows, or the allocator reports a failure, then an error
368+
/// is returned.
369+
///
370+
/// # Examples
371+
///
372+
/// ```
373+
/// #![feature(try_reserve)]
374+
/// use std::collections::HashSet;
375+
/// let mut set: HashSet<i32> = HashSet::new();
376+
/// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
377+
/// ```
378+
#[inline]
379+
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
380+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
381+
self.map.try_reserve(additional)
382+
}
383+
360384
/// Shrinks the capacity of the set as much as possible. It will drop
361385
/// down as much as possible while maintaining the internal rules
362386
/// and possibly leaving some space in accordance with the resize policy.

0 commit comments

Comments
 (0)