Start learning free

LESSON 202 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Rust: Common Passwords with HashSet

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

The Rust editor starts empty, so write the whole program yourself. Begin with the data:

let common = HashSet::from(["password", "123456", "qwerty", "letmein"]);
let password = "qwerty";

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the set common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use a HashSet<&str> and call .contains(password) (returns true if present).

let common = HashSet::from(["password", "123456", "qwerty", "letmein"]);
let password = "qwerty";

println!("Password: {}", password);
let is_common = common.contains(password);
println!("Common: {}", if is_common { "True" } else { "False" });
if is_common {
    println!("Weak: reject");
} else {
    println!("Strong: accept");
}

STEP 1 OF 7
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Rust, explained

    Problem overview

    Version 5 rejects well-known passwords. The program starts with a set of four common ones, password, 123456, qwerty and letmein, and the password to check, qwerty. It prints the password, whether it is common and a verdict:

    Password: qwerty
    Common: True
    Weak: reject
    

    For Tr0ub4dor, which is not in the set, the output is Password: Tr0ub4dor, Common: False and Strong: accept.

    Approach: ask a set

    A list of common passwords could be searched one entry at a time. A HashSet answers "is this value in here?" directly, without looking at each entry, and the answer stays fast however many passwords the set holds. The set has no values attached to its entries; it only remembers which ones are present, which is exactly what this check needs.

    The program stores the answer in a boolean, is_common, because it is used twice: for the Common line and for the verdict.

    Step by step

    1. Create common with HashSet::from([...]), passing the four passwords in an array.
    2. Create password with the text "qwerty".
    3. Print the Password line.
    4. Create is_common from common.contains(password), which is true when the set holds that text.
    5. Print the Common line, choosing "True" or "False" with an if expression, because Rust prints a bool in lowercase.
    6. Write the if on is_common and print Weak: reject.
    7. Add the else that prints Strong: accept.

    The solution in Rust

        let common = HashSet::from(["password", "123456", "qwerty", "letmein"]);
        let password = "qwerty";
    
        println!("Password: {}", password);
        let is_common = common.contains(password);
        println!("Common: {}", if is_common { "True" } else { "False" });
        if is_common {
            println!("Weak: reject");
        } else {
            println!("Strong: accept");
        }
    

    Rust details worth knowing:

    • Where HashSet comes from. It is in std::collections, so a program brings it into scope with use std::collections::HashSet;.
    • HashSet::from takes an array. The set's element type, &str here, is inferred from the strings.
    • contains takes a reference. password is already a &str, so it is passed as is.
    • A bool can be a condition directly. if is_common needs no == true.
    • The verdict is not tied to length or classes. A common password is rejected whatever else is true of it, and a password that is not common is accepted here without any other check.

    Sets and lists

    A set stores each value once. Inserting the same text twice leaves one copy: the first insert of "a" returns true, the second returns false, and the set's length stays 1. That matches the job here, because a password is either on the list or not, and a duplicate entry would add nothing.

    A Vec of the same four strings would also answer the question with contains, but it compares the password with the entries one by one. The set computes a hash of the text and goes straight to where it would be. For four entries the difference is invisible. For a list of a million common passwords it matters.

    Edge cases and common mistakes

    • Case matters. Qwerty is not in the set, so it prints Common: False and Strong: accept.
    • Whitespace matters. qwerty with a trailing space does not match either.
    • Digits-only entries. 123456 is in the set as text, so it is rejected.
    • Empty password. It is not in the set, so this version accepts it.
    • Printing the bool directly. println!("Common: {}", is_common) prints true in lowercase, which does not match the expected text.
    • Comparing against a list by hand. A chain of == tests works for four passwords but grows with every new entry; the set does not.

    Testing

    PasswordCommon lineVerdict
    qwertyCommon: TrueWeak: reject
    Tr0ub4dorCommon: FalseStrong: accept
    QwertyCommon: FalseStrong: accept
    123456Common: TrueWeak: reject
    (empty)Common: FalseStrong: accept
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free