9951 explained code solutions for 126 technologies


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:

  1. Imports the Hash and Hasher traits from the standard library.
  2. Creates a DefaultHasher instance.
  3. Hashes the string "Hello World" using the hash method.
  4. Stores the resulting hash in the hash variable.

Helpful links

Edit this code on GitHub