|
1 | 1 | use crate::borrow::Borrow;
|
| 2 | +use crate::collections::CollectionAllocErr; |
2 | 3 | use crate::fmt;
|
3 | 4 | use crate::hash::{Hash, BuildHasher};
|
4 | 5 | use crate::iter::{Chain, FromIterator, FusedIterator};
|
@@ -357,6 +358,29 @@ impl<T, S> HashSet<T, S>
|
357 | 358 | self.map.reserve(additional)
|
358 | 359 | }
|
359 | 360 |
|
| 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 | + |
360 | 384 | /// Shrinks the capacity of the set as much as possible. It will drop
|
361 | 385 | /// down as much as possible while maintaining the internal rules
|
362 | 386 | /// and possibly leaving some space in accordance with the resize policy.
|
|
0 commit comments