当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust HashSet.try_reserve用法及代码示例


本文简要介绍rust语言中 std::collections::hash_set::HashSet.try_reserve 的用法。

用法

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

尝试为至少 additional 更多要插入给定 HashSet<K, V> 的元素保留容量。集合可以保留更多空间以避免频繁的重新分配。

错误

如果容量溢出,或者分配器报告失败,则返回错误。

例子

use std::collections::HashSet;
let mut set: HashSet<i32> = HashSet::new();
set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::collections::hash_set::HashSet.try_reserve。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。