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 get execution time in Rust
- How to replace strings using Rust regex?
- How to match the end of a line in a Rust regex?
- How to get an entry from a HashSet in Rust?
- How to convert a Rust HashMap to JSON?
- Example of struct of structs in Rust
- How to create enum from string in Rust
- How to perform matrix operations in Rust?
See more codes...