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 replace all matches using Rust regex?
- How to use Unicode in a regex in Rust?
- How to split a string with Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to convert a Rust HashMap to a JSON string?
- Regex example to match multiline string in Rust?
- How to parse JSON string in Rust?
See more codes...