### xxh3_128 - One-shot 128-bit Hash Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute a 128-bit XXH3 hash for complete data, offering enhanced collision resistance. Examples show basic hashing, hashing with a seed, and extracting the high and low 64-bit parts of the hash. ```rust use xxhash_rust::xxh3::{xxh3_128, xxh3_128_with_seed}; let data = b"Hello, World!"; // Basic 128-bit hash let hash = xxh3_128(data); println!("128-bit hash: {:#034x}", hash); // With seed let hash_seeded = xxh3_128_with_seed(data, 42); println!("128-bit seeded: {:#034x}", hash_seeded); // Extract high and low parts let low = hash as u64; let high = (hash >> 64) as u64; println!("Low: {:#018x}", low); println!("High: {:#018x}", high); ``` -------------------------------- ### Basic Usage of xxhash-rust with Constant Hashing Source: https://github.com/doumanash/xxhash-rust/blob/master/README.md Demonstrates using the `const_xxh3` for compile-time hashing and `xxh3_64` for runtime hashing. Ensure the input string matches the expected hash for the assertion to pass. ```rust use xxhash_rust::const_xxh3::xxh3_64 as const_xxh3; use xxhash_rust::xxh3::xxh3_64; const TEST: u64 = const_xxh3(b"TEST"); fn test_input(text: &str) -> bool { xxh3_64(text.as_bytes()) == TEST } assert!(!test_input("tEST")); assert!(test_input("TEST")); ``` -------------------------------- ### xxh64 - One-shot 64-bit Hash (Classic) Source: https://context7.com/doumanash/xxhash-rust/llms.txt The classic 64-bit xxHash algorithm is suitable for x86_64 targets lacking XXH3 SIMD acceleration. It supports basic hashing and custom seeds. ```rust use xxhash_rust::xxh64::xxh64; let data = b"Hello, World!"; // Basic hashing with seed 0 let hash = xxh64(data, 0); println!("XXH64 hash: {:#018x}", hash); // With custom seed let hash_seeded = xxh64(data, 0xCAFEBABE); println!("XXH64 seeded: {:#018x}", hash_seeded); // Verify determinism assert_eq!(xxh64(data, 0), xxh64(data, 0)); ``` -------------------------------- ### Compile-time 64-bit Hash (Classic) Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute classic xxh64 hashes at compile time. Useful for pre-computing hash constants for string matching or lookup tables. ```rust use xxhash_rust::const_xxh64::xxh64 as const_xxh64; use xxhash_rust::xxh64::xxh64; // Compile-time constant const TABLE_NAME_HASH: u64 = const_xxh64(b"users_table", 0); const CONFIG_KEY_HASH: u64 = const_xxh64(b"max_connections", 0); // Runtime lookup fn lookup_config(key: &str) -> Option<&'static str> { match xxh64(key.as_bytes(), 0) { CONFIG_KEY_HASH => Some("100"), _ => None, } } assert_eq!(lookup_config("max_connections"), Some("100")); ``` -------------------------------- ### Xxh3 - Streaming Hasher Source: https://context7.com/doumanash/xxhash-rust/llms.txt Utilize the streaming hasher for incremental data processing, suitable for large files or data streams. Demonstrates creating a hasher, updating it with data chunks, digesting the final hash (64-bit and 128-bit), and resetting or creating with a custom seed. ```rust use xxhash_rust::xxh3::Xxh3; // Create a streaming hasher let mut hasher = Xxh3::new(); // Feed data incrementally hasher.update(b"Hello, "); hasher.update(b"World!"); hasher.update(b" More data..."); // Get the final hash let hash = hasher.digest(); println!("Streaming hash: {:#018x}", hash); // Get 128-bit hash from streaming let hash128 = hasher.digest128(); println!("Streaming 128-bit: {:#034x}", hash128); // Reset and reuse hasher.reset(); hasher.update(b"New data"); let new_hash = hasher.digest(); // With custom seed let mut seeded_hasher = Xxh3::with_seed(12345); seeded_hasher.update(b"data"); let seeded_hash = seeded_hasher.digest(); ``` -------------------------------- ### Xxh64 - Streaming 64-bit Hasher Source: https://context7.com/doumanash/xxhash-rust/llms.txt This streaming version of xxh64 implements both `core::hash::Hasher` and `std::io::Write` traits. It allows incremental hashing and can be reset for reuse. ```rust use xxhash_rust::xxh64::{Xxh64, Xxh64Builder}; use std::hash::Hasher; use std::collections::HashMap; // Basic streaming usage let mut hasher = Xxh64::new(0); hasher.update(b"Hello, "); hasher.update(b"World!"); let hash = hasher.digest(); println!("Streaming XXH64: {:#018x}", hash); // Using the Hasher trait let mut hasher = Xxh64::new(0); hasher.write(b"test"); let hash = hasher.finish(); // Reset and reuse with different seed hasher.reset(12345); hasher.update(b"new data"); let new_hash = hasher.digest(); // Use with HashMap let builder = Xxh64Builder::new(0); let mut map: HashMap, String, Xxh64Builder> = HashMap::with_hasher(builder); map.insert(vec![1, 2, 3], "value".to_string()); // With std feature: use std::io::Write #[cfg(feature = "std")] { use std::io::Write; let mut hasher = Xxh64::new(0); write!(hasher, "formatted {}", 42).unwrap(); let hash = hasher.digest(); } ``` -------------------------------- ### xxh32 - One-shot 32-bit Hash Source: https://context7.com/doumanash/xxhash-rust/llms.txt The 32-bit xxHash algorithm is efficient for 32-bit platforms where 64-bit operations are costly. It supports basic hashing and seeding. ```rust use xxhash_rust::xxh32::xxh32; let data = b"Hello, World!"; // Basic hashing let hash = xxh32(data, 0); println!("XXH32 hash: {:#010x}", hash); // With seed let hash_seeded = xxh32(data, 0xDEADBEEF); println!("XXH32 seeded: {:#010x}", hash_seeded); ``` -------------------------------- ### xxh3_64 - One-shot 64-bit Hash (XXH3) Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute a 64-bit XXH3 hash for complete data. This is the fastest xxHash variant and recommended for most use cases. Supports hashing byte slices and strings. ```rust use xxhash_rust::xxh3::xxh3_64; // Basic hashing let hash = xxh3_64(b"Hello, World!"); println!("Hash: {:#018x}", hash); // Output: Hash: 0x7b06971d3ddc0442 // Hashing strings let data = "The quick brown fox jumps over the lazy dog"; let hash = xxh3_64(data.as_bytes()); println!("Hash: {}", hash); // Hashing binary data let binary_data: Vec = (0..256).map(|i| i as u8).collect(); let hash = xxh3_64(&binary_data); println!("Binary hash: {:#018x}", hash); ``` -------------------------------- ### Xxh32 - Streaming 32-bit Hasher Source: https://context7.com/doumanash/xxhash-rust/llms.txt This streaming version of xxh32 allows incremental hashing of data in chunks. It can be reset for reuse and implements `std::io::Write` when the 'std' feature is enabled. ```rust use xxhash_rust::xxh32::Xxh32; // Create hasher with seed let mut hasher = Xxh32::new(0); // Stream data hasher.update(b"chunk1"); hasher.update(b"chunk2"); hasher.update(b"chunk3"); // Get result let hash = hasher.digest(); println!("Streaming XXH32: {:#010x}", hash); // Reset for reuse hasher.reset(42); hasher.update(b"new data"); let new_hash = hasher.digest(); // With std feature: implements std::io::Write #[cfg(feature = "std")] { use std::io::Write; let mut hasher = Xxh32::new(0); hasher.write_all(b"data via Write trait").unwrap(); let hash = hasher.digest(); } ``` -------------------------------- ### Add xxhash-rust Dependency to Cargo.toml Source: https://github.com/doumanash/xxhash-rust/blob/master/README.md Specify the xxhash-rust crate and desired features in your Cargo.toml file. Features like 'xxh3' and 'const_xxh3' enable specific hashing algorithms and compile-time computation. ```toml [dependencies.xxhash-rust] version = "0.8.12" features = ["xxh3", "const_xxh3"] ``` -------------------------------- ### Compile-time 32-bit Hash Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute xxh32 hashes at compile time, suitable for 32-bit applications. Enables fast command dispatch based on pre-computed hash constants. ```rust use xxhash_rust::const_xxh32::xxh32 as const_xxh32; use xxhash_rust::xxh32::xxh32; // Compile-time hash constants const CMD_HELP: u32 = const_xxh32(b"help", 0); const CMD_VERSION: u32 = const_xxh32(b"version", 0); const CMD_RUN: u32 = const_xxh32(b"run", 0); // Fast command dispatch fn dispatch_command(cmd: &str) { match xxh32(cmd.as_bytes(), 0) { CMD_HELP => println!("Showing help..."), CMD_VERSION => println!("Version 1.0.0"), CMD_RUN => println!("Running..."), _ => println!("Unknown command"), } } dispatch_command("help"); dispatch_command("version"); dispatch_command("unknown"); ``` -------------------------------- ### xxh3_64_with_seed - 64-bit Hash with Custom Seed Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute a 64-bit XXH3 hash using a custom seed. Using different seeds for the same input generates different hash values, useful for scenarios like hash table implementations requiring multiple independent hash functions. ```rust use xxhash_rust::xxh3::xxh3_64_with_seed; let data = b"test data"; // Different seeds produce different hashes let hash1 = xxh3_64_with_seed(data, 0); let hash2 = xxh3_64_with_seed(data, 12345); let hash3 = xxh3_64_with_seed(data, 0xDEADBEEF); println!("Seed 0: {:#018x}", hash1); println!("Seed 12345: {:#018x}", hash2); println!("Seed 0xDEADBEEF: {:#018x}", hash3); // Use case: Creating hash table with different hash functions fn double_hashing(key: &[u8], table_size: usize, attempt: u64) -> usize { let h1 = xxh3_64_with_seed(key, 0) as usize; let h2 = xxh3_64_with_seed(key, 1) as usize; (h1 + attempt as usize * h2) % table_size } ``` -------------------------------- ### Xxh3Default - Optimized Default Streaming Hasher Source: https://context7.com/doumanash/xxhash-rust/llms.txt Use Xxh3Default for optimized streaming hashing, especially in hash maps where frequent instance creation occurs. It utilizes default seed and secret values. ```rust use xxhash_rust::xxh3::{Xxh3Default, Xxh3DefaultBuilder}; use std::collections::HashMap; use std::hash::BuildHasher; // Direct usage let mut hasher = Xxh3Default::new(); hasher.update(b"key data"); let hash = hasher.digest(); // Use with HashMap via BuildHasher let builder = Xxh3DefaultBuilder::new(); let mut map: HashMap = HashMap::with_hasher(builder); map.insert("key1".to_string(), 100); map.insert("key2".to_string(), 200); println!("key1 value: {:?}", map.get("key1")); println!("key2 value: {:?}", map.get("key2")); ``` -------------------------------- ### xxh3_64_with_secret - 64-bit Hash with Custom Secret Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute a 64-bit XXH3 hash using a custom 192-byte secret. This ensures deterministic hashing across different systems when a specific secret is required. The secret can be generated at compile time using `const_custom_default_secret`. ```rust use xxhash_rust::xxh3::{xxh3_64_with_secret, DEFAULT_SECRET_SIZE}; use xxhash_rust::const_xxh3::const_custom_default_secret; // Generate a custom secret at compile time from a seed const MY_SECRET: [u8; DEFAULT_SECRET_SIZE] = const_custom_default_secret(42); let data = b"sensitive data"; let hash = xxh3_64_with_secret(data, &MY_SECRET); println!("Hash with custom secret: {:#018x}", hash); // The same secret produces consistent hashes let hash2 = xxh3_64_with_secret(data, &MY_SECRET); assert_eq!(hash, hash2); ``` -------------------------------- ### const_xxh3::xxh3_64 - Compile-time 64-bit Hash Source: https://context7.com/doumanash/xxhash-rust/llms.txt Compute xxh3 hashes at compile time using `const fn`. This is useful for creating compile-time hash constants for comparisons, including seeded and 128-bit variants. ```rust use xxhash_rust::const_xxh3::xxh3_64 as const_xxh3; use xxhash_rust::xxh3::xxh3_64; // Compute hash at compile time const EXPECTED_HASH: u64 = const_xxh3(b"expected_value"); const PASSWORD_HASH: u64 = const_xxh3(b"secret_token"); // Runtime comparison function fn verify_input(input: &str) -> bool { xxh3_64(input.as_bytes()) == EXPECTED_HASH } // Usage assert!(verify_input("expected_value")); assert!(!verify_input("wrong_value")); // With seed (compile-time) use xxhash_rust::const_xxh3::xxh3_64_with_seed; const SEEDED_HASH: u64 = xxh3_64_with_seed(b"data", 42); // 128-bit compile-time hash use xxhash_rust::const_xxh3::xxh3_128; const HASH_128: u128 = xxh3_128(b"test"); ``` -------------------------------- ### const_xxh3::const_custom_default_secret - Generate Custom Secret at Compile Time Source: https://context7.com/doumanash/xxhash-rust/llms.txt Generate a custom secret derived from a seed at compile time using `const_custom_default_secret`. This is more efficient than computing the secret at runtime for hashing with a custom secret. ```rust use xxhash_rust::const_xxh3::const_custom_default_secret; use xxhash_rust::xxh3::{xxh3_64_with_secret, DEFAULT_SECRET_SIZE}; // Generate secret at compile time const MY_SECRET: [u8; DEFAULT_SECRET_SIZE] = const_custom_default_secret(0xCAFEBABE); fn hash_with_app_secret(data: &[u8]) -> u64 { xxh3_64_with_secret(data, &MY_SECRET) } let hash = hash_with_app_secret(b"application data"); println!("Hash: {:#018x}", hash); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.