### Using FxHashMap in Rust Source: https://github.com/rust-lang/rustc-hash/blob/master/README.md Demonstrates basic usage of FxHashMap for key-value storage. Ensure the rustc_hash crate is added as a dependency. ```rust use rustc_hash::FxHashMap; let mut map: FxHashMap = FxHashMap::default(); map.insert(22, 44); ``` -------------------------------- ### Using FxBuildHasher with HashMap Source: https://context7.com/rust-lang/rustc-hash/llms.txt Shows how to use FxBuildHasher as a zero-sized BuildHasher for creating FxHasher instances with a default seed. It's suitable for hash collections that accept a custom BuildHasher. ```rust use std::hash::BuildHasher; use rustc_hash::FxBuildHasher; fn main() { // Use BuildHasher::hash_one for one-off hashing without constructing a map let h1 = FxBuildHasher.hash_one(1_u32); let h2 = FxBuildHasher.hash_one(2_u32); assert_ne!(h1, h2); println!("hash(1) = {h1}"); println!("hash(2) = {h2}"); // Use with HashMap::with_hasher let mut map = std::collections::HashMap::with_hasher(FxBuildHasher); map.insert("x", 10); map.insert("y", 20); println!("{:?}", map.get("x")); // Some(10) // Use with a third-party IndexMap (e.g., indexmap crate) // let mut index_map: indexmap::IndexMap<&str, i32, FxBuildHasher> = // indexmap::IndexMap::with_hasher(FxBuildHasher); } ``` -------------------------------- ### Using FxHasher for Hashing Primitive Types and Custom Data Source: https://context7.com/rust-lang/rustc-hash/llms.txt Demonstrates how to use FxHasher for hashing primitive types, strings, and custom data by manually feeding bytes. Supports construction with a default or explicit seed. ```rust use core::hash::{Hash, Hasher}; use rustc_hash::FxHasher; fn fx_hash(value: &T) -> u64 { let mut hasher = FxHasher::default(); value.hash(&mut hasher); hasher.finish() } fn fx_hash_seeded(value: &T, seed: usize) -> u64 { let mut hasher = FxHasher::with_seed(seed); value.hash(&mut hasher); hasher.finish() } fn main() { // Hash primitive types println!("{}", fx_hash(&42_u32)); // deterministic u64 println!("{}", fx_hash(&"hello world")); // string hashing // Same value, different seeds → different hashes let h1 = fx_hash_seeded(&"key", 0); let h2 = fx_hash_seeded(&"key", 1); assert_ne!(h1, h2); println!("seed 0: {h1}, seed 1: {h2}"); // Manually feed bytes for a custom type let mut hasher = FxHasher::default(); hasher.write_u32(0xDEAD_BEEF); hasher.write_u64(0x0102_0304_0506_0708); let result = hasher.finish(); println!("manual hash: {result}"); } ``` -------------------------------- ### Manual Hashing in a No-std Context with FxHasher Source: https://context7.com/rust-lang/rustc-hash/llms.txt Illustrates manual hashing of a value using FxHasher in a no_std context. This is useful when you need to hash individual items without using a collection. ```rust use core::hash::{Hash, Hasher}; use rustc_hash::FxHasher; fn hash_value(v: &T) -> u64 { let mut h = FxHasher::default(); v.hash(&mut h); h.finish() } ``` -------------------------------- ### No-std Usage of FxBuildHasher with hashbrown Source: https://context7.com/rust-lang/rustc-hash/llms.txt Shows how to use FxBuildHasher in a no_std environment with the hashbrown crate. This is common for embedded systems and WebAssembly targets requiring compact, fast associative storage without the standard library. ```toml # Cargo.toml [dependencies] rustc-hash = { version = "2.1", default-features = false } hashbrown = "0.14" ``` ```rust #!/usr/bin/env rust #![no_std] use rustc_hash::FxBuildHasher; use hashbrown::HashMap; fn build_map() -> HashMap { let mut map = HashMap::with_hasher(FxBuildHasher); map.insert(1, 100); map.insert(2, 200); map } ``` -------------------------------- ### Using FxRandomState for Random-Seeded HashMaps and HashSets Source: https://context7.com/rust-lang/rustc-hash/llms.txt Demonstrates the usage of FxRandomState with FxHashMapRand and FxHashSetRand for per-instance random seeds. This prevents HashDoS correlation by ensuring different map instances have different hash seeds. Explicit construction via FxRandomState::new() is also shown. ```rust // Cargo.toml: rustc-hash = { version = "2.1", features = ["rand"] } use rustc_hash::{FxRandomState, FxHashMapRand, FxHashSetRand}; fn main() { // FxHashMapRand: HashMap let mut map_a: FxHashMapRand<&str, i32> = FxHashMapRand::default(); let mut map_b: FxHashMapRand<&str, i32> = FxHashMapRand::default(); map_a.insert("hello", 1); map_b.insert("hello", 1); // Both maps store "hello" → 1 correctly … assert_eq!(map_a[&"hello"], 1); assert_eq!(map_b[&"hello"], 1); // … but their internal hash seeds differ, preventing HashDoS correlation assert_ne!(map_a.hasher().build_hasher().finish(), map_b.hasher().build_hasher().finish()); // FxHashSetRand: HashSet let mut set: FxHashSetRand = FxHashSetRand::default(); set.insert(1); set.insert(2); set.insert(3); println!("set contains 2: {}", set.contains(&2)); // true // Explicit construction via FxRandomState::new() let state = FxRandomState::new(); let mut manual: std::collections::HashMap = std::collections::HashMap::with_hasher(state); manual.insert(7, 49); assert_eq!(manual[&7], 49); } ``` -------------------------------- ### Using FxHashMap for Fast Hash Maps Source: https://context7.com/rust-lang/rustc-hash/llms.txt FxHashMap provides a drop-in replacement for std::collections::HashMap with improved performance. It uses FxBuildHasher with a zero-seed by default for deterministic hashing. ```rust use rustc_hash::FxHashMap; fn main() { // Create with Default::default() — zero-cost, no allocation let mut scores: FxHashMap<&str, u32> = FxHashMap::default(); scores.insert("alice", 42); scores.insert("bob", 17); scores.insert("carol", 99); // Standard HashMap API works unchanged if let Some(score) = scores.get("alice") { println!("alice: {score}"); // alice: 42 } scores.entry("dave").or_insert(0); *scores.entry("alice").or_insert(0) += 8; println!("alice updated: {}", scores["alice"]); // alice updated: 50 // Pre-allocate capacity when size is known let mut lookup: FxHashMap = FxHashMap::with_capacity_and_hasher( 1024, rustc_hash::FxBuildHasher, ); for i in 0..100_u32 { lookup.insert(i, format!("item-{i}")); } println!("lookup has {} entries", lookup.len()); // lookup has 100 entries } ``` -------------------------------- ### Deterministic Hashing with FxSeededState Source: https://context7.com/rust-lang/rustc-hash/llms.txt Illustrates using FxSeededState to create FxHasher instances with a caller-supplied seed, ensuring deterministic hash outputs for reproducible builds or snapshot tests. ```rust use std::collections::HashMap; use rustc_hash::{FxSeededState, FxHashMapSeed}; fn main() { // Construct directly with HashMap::with_hasher let mut map: HashMap<&str, u32, FxSeededState> = HashMap::with_hasher(FxSeededState::with_seed(42)); map.insert("alpha", 1); map.insert("beta", 2); assert_eq!(map[&"alpha"], 1); // Use the FxHashMapSeed type alias (requires std feature) let mut seeded: FxHashMapSeed = HashMap::with_hasher(FxSeededState::with_seed(0xCAFE_BABE)); seeded.insert(1, "one"); seeded.insert(2, "two"); // Two maps with the same seed hash keys identically → same iteration // order for equal key sets (implementation-defined, but deterministic) let clone = seeded.clone(); assert_eq!(clone[&1], "one"); // Different seeds produce different hash values for the same key use core::hash::BuildHasher; let s1 = FxSeededState::with_seed(1); let s2 = FxSeededState::with_seed(2); assert_ne!(s1.hash_one(99_u64), s2.hash_one(99_u64)); println!("seed 1 hash: {}", s1.hash_one(99_u64)); println!("seed 2 hash: {}", s2.hash_one(99_u64)); } ``` -------------------------------- ### Add rustc-hash to Cargo.toml Source: https://context7.com/rust-lang/rustc-hash/llms.txt Choose the appropriate dependency configuration for your project's needs, including standard, no_std, or random-seed support. ```toml # Standard (includes FxHashMap / FxHashSet collection aliases) [dependencies] rustc-hash = "2.1" ``` ```toml # no_std (hasher only, no collection aliases) [dependencies] rustc-hash = { version = "2.1", default-features = false } ``` ```toml # With random-seed support (requires rand 0.8) [dependencies] rustc-hash = { version = "2.1", features = ["rand"] } ``` -------------------------------- ### FxBuildHasher - BuildHasher Unit Struct Source: https://context7.com/rust-lang/rustc-hash/llms.txt FxBuildHasher is a zero-sized BuildHasher that produces FxHasher instances with the default zero seed. It is Copy, Clone, and Default, making it suitable as a const-compatible hasher factory for hash collections. ```APIDOC ## `FxBuildHasher` — BuildHasher Unit Struct `FxBuildHasher` is a zero-sized `BuildHasher` that produces `FxHasher` instances with the default zero seed. Because it is `Copy`, `Clone`, and `Default`, it can be used as a const-compatible hasher factory for any standard or third-party hash collection that accepts a custom `BuildHasher`. ```rust use std::hash::BuildHasher; use rustc_hash::FxBuildHasher; fn main() { // Use BuildHasher::hash_one for one-off hashing without constructing a map let h1 = FxBuildHasher.hash_one(1_u32); let h2 = FxBuildHasher.hash_one(2_u32); assert_ne!(h1, h2); println!("hash(1) = {{h1}}"); println!("hash(2) = {{h2}}"); // Use with HashMap::with_hasher let mut map = std::collections::HashMap::with_hasher(FxBuildHasher); map.insert("x", 10); map.insert("y", 20); println!("{:?}", map.get("x")); // Some(10) // Use with a third-party IndexMap (e.g., indexmap crate) // let mut index_map: indexmap::IndexMap<&str, i32, FxBuildHasher> = // indexmap::IndexMap::with_hasher(FxBuildHasher); } ``` ``` -------------------------------- ### Using FxHashSet for Fast Hash Sets Source: https://context7.com/rust-lang/rustc-hash/llms.txt FxHashSet offers a high-performance alternative to std::collections::HashSet for set operations. It shares the same performance characteristics as FxHashMap. ```rust use rustc_hash::FxHashSet; fn deduplicate(items: &[u32]) -> FxHashSet { items.iter().copied().collect() } fn main() { let data = vec![1, 2, 3, 2, 4, 1, 5, 3]; let unique = deduplicate(&data); println!("unique count: {}", unique.len()); // unique count: 5 println!("contains 3: {}", unique.contains(&3)); // contains 3: true println!("contains 9: {}", unique.contains(&9)); // contains 9: false let a: FxHashSet = [1, 2, 3, 4].iter().copied().collect(); let b: FxHashSet = [3, 4, 5, 6].iter().copied().collect(); let intersection: FxHashSet<_> = a.intersection(&b).copied().collect(); println!("intersection: {:?}", { let mut v: Vec<_> = intersection.into_iter().collect(); v.sort(); v }); // intersection: [3, 4] } ``` -------------------------------- ### FxHasher - Raw Hasher Source: https://context7.com/rust-lang/rustc-hash/llms.txt FxHasher implements the standard Hasher trait and can be used directly. It supports construction with a default zero seed or an explicit seed. The finish() method returns a rotated u64 hash value. ```APIDOC ## `FxHasher` — Raw Hasher `FxHasher` implements `std::hash::Hasher` directly and can be used wherever a `Hasher` is required. It supports construction with an explicit seed via `FxHasher::with_seed(seed)` or the default zero-seed via `FxHasher::default()`. The `finish()` method returns a `u64` hash value rotated left by 26 bits (64-bit hosts) or 15 bits (32-bit hosts) to improve distribution for hash-table bucket indexing. ```rust use core::hash::{Hash, Hasher}; use rustc_hash::FxHasher; fn fx_hash(value: &T) -> u64 { let mut hasher = FxHasher::default(); value.hash(&mut hasher); hasher.finish() } fn fx_hash_seeded(value: &T, seed: usize) -> u64 { let mut hasher = FxHasher::with_seed(seed); value.hash(&mut hasher); hasher.finish() } fn main() { // Hash primitive types println!("{}", fx_hash(&42_u32)); // deterministic u64 println!("{}", fx_hash(&"hello world")); // string hashing // Same value, different seeds → different hashes let h1 = fx_hash_seeded(&"key", 0); let h2 = fx_hash_seeded(&"key", 1); assert_ne!(h1, h2); println!("seed 0: {{h1}}, seed 1: {{h2}}"); // Manually feed bytes for a custom type let mut hasher = FxHasher::default(); hasher.write_u32(0xDEAD_BEEF); hasher.write_u64(0x0102_0304_0506_0708); let result = hasher.finish(); println!("manual hash: {{result}}"); } ``` ``` -------------------------------- ### Disabling std Feature for rustc-hash Source: https://github.com/rust-lang/rustc-hash/blob/master/README.md Shows how to disable the default 'std' feature for the rustc-hash crate in Cargo.toml, typically for no_std environments. ```toml rustc-hash = { version = "2.1", default-features = false } ``` -------------------------------- ### FxSeededState - Deterministic Seeded State Source: https://context7.com/rust-lang/rustc-hash/llms.txt FxSeededState is a BuildHasher that creates FxHasher instances initialized with a caller-supplied seed, ensuring deterministic hashing for reproducible builds and snapshot tests. ```APIDOC ## `FxSeededState` — Deterministic Seeded State `FxSeededState` is a `BuildHasher` that creates `FxHasher` instances initialized with a caller-supplied seed. Two `FxSeededState` instances constructed with the same seed will produce identical hash outputs for identical keys, making this suitable for reproducible builds, snapshot tests, or any scenario that requires deterministic hashing without relying on the zero default. ```rust use std::collections::HashMap; use rustc_hash::{FxSeededState, FxHashMapSeed}; fn main() { // Construct directly with HashMap::with_hasher let mut map: HashMap<&str, u32, FxSeededState> = HashMap::with_hasher(FxSeededState::with_seed(42)); map.insert("alpha", 1); map.insert("beta", 2); assert_eq!(map[&"alpha"], 1); // Use the FxHashMapSeed type alias (requires std feature) let mut seeded: FxHashMapSeed = HashMap::with_hasher(FxSeededState::with_seed(0xCAFE_BABE)); seeded.insert(1, "one"); seeded.insert(2, "two"); // Two maps with the same seed hash keys identically → same iteration // order for equal key sets (implementation-defined, but deterministic) let clone = seeded.clone(); assert_eq!(clone[&1], "one"); // Different seeds produce different hash values for the same key use core::hash::BuildHasher; let s1 = FxSeededState::with_seed(1); let s2 = FxSeededState::with_seed(2); assert_ne!(s1.hash_one(99_u64), s2.hash_one(99_u64)); println!("seed 1 hash: {{}}", s1.hash_one(99_u64)); println!("seed 2 hash: {{}}", s2.hash_one(99_u64)); } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.