Start learning free

LESSON 199 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in Rust: Length and a Digit

Practice Read the full explanation

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

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

let password = "Secret99";

Build:

Length: 8
Has digit: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag has_digit, which starts as false and becomes true when a digit is seen.
  • Print "Has digit: " 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 has_digit is true, print Strong; otherwise print Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

c.is_ascii_digit() works on a char.

let password = "Secret99";

let length = password.len();
println!("Length: {}", length);
let mut has_digit = false;
for character in password.chars() {
    if character.is_ascii_digit() {
        has_digit = true;
    }
}
println!("Has digit: {}", if has_digit { "True" } else { "False" });
if length >= 8 && has_digit {
    println!("Strong");
} else {
    println!("Weak");
}

STEP 1 OF 9
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in Rust, explained

    Problem overview

    Version 2 adds a second rule. A password is Strong only if it is at least 8 long and contains at least one digit; otherwise it is Weak. For Secret99 the program prints:

    Length: 8
    Has digit: True
    Strong
    

    For abcdefgh the second line is Has digit: False and the verdict is Weak. Note the capitalized True and False: the output format asks for them.

    Approach: a flag set by a loop

    To find out whether any character is a digit, scan the whole password with a boolean flag, has_digit, that starts as false. Every time the loop sees a digit, it sets the flag to true. Nothing ever sets it back, so after the loop the flag answers "was there at least one digit?". The verdict then combines two conditions with &&.

    Step by step

    1. Create password with the text Secret99.
    2. Create length from password.len().
    3. Print the Length line with println!; each {} in its text is filled by a value listed after it.
    4. Create has_digit with let mut, starting at false.
    5. Loop with for character in password.chars(), which hands out the characters one at a time.
    6. Test each character with character.is_ascii_digit() and set has_digit to true when it passes.
    7. Print the Has digit line after the loop. A bool prints as lowercase true, so the text is chosen with if has_digit { "True" } else { "False" }.
    8. Write the verdict: if length >= 8 && has_digit prints Strong.
    9. Add the else that prints Weak. It catches every other case: too short, no digit, or both.

    The solution in Rust

        let password = "Secret99";
    
        let length = password.len();
        println!("Length: {}", length);
        let mut has_digit = false;
        for character in password.chars() {
            if character.is_ascii_digit() {
                has_digit = true;
            }
        }
        println!("Has digit: {}", if has_digit { "True" } else { "False" });
        if length >= 8 && has_digit {
            println!("Strong");
        } else {
            println!("Weak");
        }
    

    Rust details worth knowing:

    • if is an expression. if has_digit { "True" } else { "False" } produces a value, so it can sit inside println! as an argument. Both branches must have the same type, here &str, and neither ends with a semicolon.
    • Booleans print in lowercase. println!("{}", true) prints true, which is why the lesson picks its own text.
    • chars() yields char values (Unicode characters), one per loop pass, while len() counts bytes.
    • is_ascii_digit() accepts only 0 to 9. A digit from another script, such as the Arabic-Indic ٣, gives false.
    • && short-circuits. If the length test fails, the second operand is not evaluated; with two plain variables that makes no visible difference.

    Tracing the flag

    Here is the flag after each character of Secret99:

    S -> has_digit = false
    e -> has_digit = false
    c -> has_digit = false
    r -> has_digit = false
    e -> has_digit = false
    t -> has_digit = false
    9 -> has_digit = true
    9 -> has_digit = true
    

    The flag flips at the first 9 and stays true when the second 9 arrives. That is why the loop only ever sets it to true: a later non-digit must not undo the result. For abcdefgh the flag never changes, and the verdict falls through to Weak.

    The loop doesn't stop at the first digit. It would be possible to break there, but for short passwords the full pass costs nothing and the code stays simpler.

    A shorter way to ask the same question

    The loop makes the flag explicit, which is the point of this lesson. Rust's iterators can also answer "does any character pass this test?" in one expression:

    let has_digit = password.chars().any(|c| c.is_ascii_digit());
    

    For Secret99 this gives true, and for abcdefgh and for the empty string it gives false. It stops at the first digit it finds. The loop version is longer, but it shows what any does underneath, and the same flag-and-loop pattern extends to the next versions, where one pass sets several flags.

    Reading the verdict

    The verdict is an if/else whose condition combines two facts with &&. Both sides must be true for Strong. The else does not repeat that logic: it covers every case the if did not take, which is exactly "too short, no digit, or both". Writing the failing cases out one by one would be longer and easy to get wrong.

    Why True is written by hand

    Printing the flag directly with println!("Has digit: {}", has_digit) would show true or false in lowercase, because that is how Rust displays a bool. The lesson's output uses a capital letter, so the program chooses between two string literals instead. This is a good use of if as an expression: the whole if ... else ... evaluates to one of the two strings, and the placeholder receives it. The pattern comes back in every later version that prints a flag.

    Reading the flag

    The flag is a summary of the whole loop in one value. After the loop, the rest of the program no longer needs to know which character was a digit or how many there were, only whether there was one.

    Edge cases and common mistakes

    • Resetting the flag in the loop. Adding an else that sets has_digit back to false would keep only the last character's answer.
    • Printing before the loop ends. The Has digit line belongs after the loop, so it runs once with the final value.
    • Digits only. 12345678 has length 8 and a digit, so it is Strong. The rules ask for nothing more.
    • Short with a digit. a1 has a digit but length 2, so it is Weak.
    • Empty password. The loop never runs; the output is Length: 0, Has digit: False, Weak.
    • Using || instead of &&. That would call abcdefgh strong, which fails the digit rule.

    Testing

    PasswordHas digit lineVerdict
    Secret99Has digit: TrueStrong
    abcdefghHas digit: FalseWeak
    a1Has digit: TrueWeak
    12345678Has digit: TrueStrong
    (empty)Has digit: 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