rustHow do I hash a string in Rust?
Hashing a string in Rust can be done using the hash trait from the standard library.
Example code
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
"Hello World".hash(&mut hasher);
let hash = hasher.finish();
Output example
hash = 8450045994500459945
The code above does the following:
- Imports the
HashandHashertraits from the standard library. - Creates a
DefaultHasherinstance. - Hashes the string "Hello World" using the
hashmethod. - Stores the resulting hash in the
hashvariable.
Helpful links
More of Rust
- How to replace a capture group using Rust regex?
- Yield example in Rust
- How to convert a u8 slice to a hex string in Rust?
- How to convert a Rust slice of u8 to u32?
- How to use look behind in regex in Rust?
- How to perform matrix operations in Rust?
- How to modify an existing entry in a Rust HashMap?
- How to create a slice from a vec in Rust?
- Using box hashmap in Rust
- How to check if a Rust HashMap contains a key?
See more codes...