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
Hash
andHasher
traits from the standard library. - Creates a
DefaultHasher
instance. - Hashes the string "Hello World" using the
hash
method. - Stores the resulting hash in the
hash
variable.
Helpful links
More of Rust
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to use regex with bytes in Rust?
- How to replace strings using Rust regex?
- How to use an enum in a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
- How to yield a thread in Rust?
- How to convert a Rust slice of u8 to a string?
- How to convert a Rust slice to a fixed array?
- How to convert a Rust HashMap to JSON?
See more codes...