本文簡要介紹rust語言中 std::collections::hash_set::HashSet.with_capacity_and_hasher 的用法。
用法
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S>創建具有指定容量的空 HashSet,使用 hasher 對鍵進行哈希處理。
哈希集將能夠保存至少 capacity 元素而無需重新分配。如果capacity 為0,則不會分配哈希集。
警告:hasher 通常是隨機生成的,其設計目的是讓 HashSet 能夠抵抗導致許多衝突和性能非常差的攻擊。使用此函數手動設置可能會暴露 DoS 攻擊向量。
傳遞的 hash_builder 應該實現 BuildHasher 特征,以便 HashMap 有用,有關詳細信息,請參閱其文檔。
例子
use std::collections::HashSet;
use std::collections::hash_map::RandomState;
let s = RandomState::new();
let mut set = HashSet::with_capacity_and_hasher(10, s);
set.insert(1);相關用法
- Rust HashSet.with_capacity用法及代碼示例
- Rust HashSet.with_hasher用法及代碼示例
- Rust HashSet.get_or_insert_with用法及代碼示例
- Rust HashSet.insert用法及代碼示例
- Rust HashSet.get_or_insert用法及代碼示例
- Rust HashSet.is_superset用法及代碼示例
- Rust HashSet.shrink_to用法及代碼示例
- Rust HashSet.difference用法及代碼示例
- Rust HashSet.try_reserve用法及代碼示例
- Rust HashSet.hasher用法及代碼示例
- Rust HashSet.drain_filter用法及代碼示例
- Rust HashSet.get用法及代碼示例
- Rust HashSet.is_empty用法及代碼示例
- Rust HashSet.shrink_to_fit用法及代碼示例
- Rust HashSet.len用法及代碼示例
- Rust HashSet.drain用法及代碼示例
- Rust HashSet.new用法及代碼示例
- Rust HashSet.remove用法及代碼示例
- Rust HashSet.reserve用法及代碼示例
- Rust HashSet.contains用法及代碼示例
- Rust HashSet.replace用法及代碼示例
- Rust HashSet.clear用法及代碼示例
- Rust HashSet.union用法及代碼示例
- Rust HashSet.is_subset用法及代碼示例
- Rust HashSet.take用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::collections::hash_set::HashSet.with_capacity_and_hasher。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
