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 use regex to match a double quote in Rust?
- How to create a HashMap of structs in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to modify an existing entry in a Rust HashMap?
- How do I identify unused variables in Rust?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use a Rust HashMap in a struct?
- How do I use a variable number of arguments in Rust?
See more codes...