Start learning free

LESSON 200 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in Rust: Four Character Classes

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

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

let password = "Secret!99";

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Start four flags (has_upper, has_lower, has_digit, has_symbol) at false. Loop over every character and set the matching flag to true.
  • A symbol is any character that is not a letter and not a digit.
  • Print one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag. 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 the length is at least 8 and all four flags are true (stricter than the last lesson), print Strong; otherwise print Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

Use c.is_ascii_uppercase(), c.is_ascii_lowercase(), c.is_ascii_digit(), c.is_ascii_alphanumeric().

let password = "Secret!99";

let length = password.len();
println!("Length: {}", length);
let mut has_upper = false;
let mut has_lower = false;
let mut has_digit = false;
let mut has_symbol = false;
for character in password.chars() {
    if character.is_ascii_uppercase() {
        has_upper = true;
    }
    if character.is_ascii_lowercase() {
        has_lower = true;
    }
    if character.is_ascii_digit() {
        has_digit = true;
    }
    if !character.is_ascii_alphanumeric() {
        has_symbol = true;
    }
}
println!("Upper: {}", if has_upper { "True" } else { "False" });
println!("Lower: {}", if has_lower { "True" } else { "False" });
println!("Digit: {}", if has_digit { "True" } else { "False" });
println!("Symbol: {}", if has_symbol { "True" } else { "False" });
if length >= 8 && has_upper && has_lower && has_digit && has_symbol {
    println!("Strong");
} else {
    println!("Weak");
}

STEP 1 OF 14
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in Rust, explained

    Problem overview

    Version 3 checks four kinds of character: an uppercase letter, a lowercase letter, a digit and a symbol, meaning anything that is neither a letter nor a digit. For Secret!99 the program prints a length, one line per flag and a verdict:

    Length: 9
    Upper: True
    Lower: True
    Digit: True
    Symbol: True
    Strong
    

    A password is Strong only if its length is at least 8 and all four flags are true. This is stricter than version 2, which asked for just a digit. For secret99 the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.

    Approach: four flags, one pass

    Version 2's idea scales: each class gets its own boolean flag, starting at false, and one loop over the characters tests all four. Each test is a separate if, not an else if chain. A character belongs to at most one class, so the chain would work here, but separate tests keep each rule independent and easy to read. After the loop the flags describe the whole password.

    Step by step

    1. Create password with the text Secret!99.
    2. Create length from password.len().
    3. Print the Length line.
    4. Create has_upper and has_lower with let mut, both false.
    5. Create has_digit and has_symbol the same way.
    6. Start the loop with for character in password.chars().
    7. Uppercase test: character.is_ascii_uppercase() sets has_upper.
    8. Lowercase test: character.is_ascii_lowercase() sets has_lower.
    9. Digit test: character.is_ascii_digit() sets has_digit.
    10. Symbol test: !character.is_ascii_alphanumeric() sets has_symbol. is_ascii_alphanumeric() is true for letters and digits, so negating it leaves everything else.
    11. After the loop, print the four flag lines in the order Upper, Lower, Digit, Symbol, each using the if that gives "True" or "False".
    12. Write the verdict: five conditions joined with &&: the length and the four flags.
    13. Add the else that prints Weak.

    The solution in Rust

        let password = "Secret!99";
    
        let length = password.len();
        println!("Length: {}", length);
        let mut has_upper = false;
        let mut has_lower = false;
        let mut has_digit = false;
        let mut has_symbol = false;
        for character in password.chars() {
            if character.is_ascii_uppercase() {
                has_upper = true;
            }
            if character.is_ascii_lowercase() {
                has_lower = true;
            }
            if character.is_ascii_digit() {
                has_digit = true;
            }
            if !character.is_ascii_alphanumeric() {
                has_symbol = true;
            }
        }
        println!("Upper: {}", if has_upper { "True" } else { "False" });
        println!("Lower: {}", if has_lower { "True" } else { "False" });
        println!("Digit: {}", if has_digit { "True" } else { "False" });
        println!("Symbol: {}", if has_symbol { "True" } else { "False" });
        if length >= 8 && has_upper && has_lower && has_digit && has_symbol {
            println!("Strong");
        } else {
            println!("Weak");
        }
    

    Rust details worth knowing:

    • Flags can be used directly as conditions. has_upper is already a bool, so && has_upper is right and == true would be redundant.
    • The is_ascii_* methods are ASCII only. They look at the English letters A to Z and a to z and the digits 0 to 9. A letter such as é is none of them, so it counts as a symbol here: Sécret99 prints Symbol: True and is Strong.
    • The ! operator negates a bool. It also works on the call result directly: !character.is_ascii_alphanumeric().
    • Four mutable bools are fine for a small check like this. Each is assigned only when its test passes and never reset.
    • The space is a symbol. Secret 99 has no !, yet Symbol: True, because a space is not alphanumeric.

    Tracing the four flags

    A short password makes the flow visible. For Sec!9 the four flags change like this:

    S -> upper=true lower=false digit=false symbol=false
    e -> upper=true lower=true digit=false symbol=false
    c -> upper=true lower=true digit=false symbol=false
    ! -> upper=true lower=true digit=false symbol=true
    9 -> upper=true lower=true digit=true symbol=true
    

    Each character turns on at most one flag, and a flag that is already true stays true. After the loop all four are true. This password still ends as Weak, because its length is 5 and the verdict needs at least 8 as well.

    The verdict needs the length and the four flags together. Writing it as one if with five conditions joined by && keeps the rule readable: every condition must hold, and any single failure sends the password to the else.

    Symbol or punctuation

    The lesson defines a symbol as anything that is not a letter and not a digit, and the code follows it with !character.is_ascii_alphanumeric(). Rust also has is_ascii_punctuation(), but it is a different rule: it is false for a space. For Secret 99, "any punctuation" is false, whereas "any character that is not alphanumeric" is true. For Secret!99 both are true. The two rules agree on the usual symbols such as ! and #, and differ on spaces. The lesson's version counts a space as a symbol.

    Why each test is its own if

    Four separate if blocks inside the loop mean that adding a fifth class later is a copy of one block. A chain of else if would also be correct today, but then every new rule would have to be placed with care, because a chain stops at the first test that passes.

    Edge cases and common mistakes

    • Missing one class. Secret99 has everything but a symbol, so it is Weak.
    • Long enough but incomplete. secret99 has length 8 and still fails because of the upper and symbol flags.
    • All classes but too short. Sec!99 has all four flags true, but its length is 6, so it is Weak.
    • Symbol test written without !. Testing is_ascii_alphanumeric() alone would flag letters and digits as symbols.
    • Printing flags inside the loop. The flag lines belong after the loop.
    • Empty password. All four flags are False and the verdict is Weak.

    Reading the output as a checklist

    The four flag lines read like a checklist the password is held up against. A Weak verdict does not say which rule failed, but the lines above it do: a single False is the missing class. Printing the evidence before the verdict is what makes the output useful to someone choosing a better password.

    Testing

    PasswordLengthFlags (Upper, Lower, Digit, Symbol)Verdict
    Secret!999True, True, True, TrueStrong
    secret998False, True, True, FalseWeak
    Secret998True, True, True, FalseWeak
    Sec!996True, True, True, TrueWeak
    Secret 999True, True, True, TrueStrong
    (empty)0False, False, False, FalseWeak
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free