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 get a capture group using Rust regex?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to borrow as static in Rust
- How to match the end of a line in a Rust regex?
- How to replace all matches using Rust regex?
- How to use Unicode in a regex in Rust?
- How to match a URL with a regex in Rust?
- How to split a string with Rust regex?
- How to use a custom hash function with a Rust HashMap?
See more codes...