Start learning free

LESSON 198 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Rust: A Length Check

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

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

let password = "Secret99";

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

password.len() gives the length (in bytes, which equals the characters for this ASCII password).

let password = "Secret99";
let length = password.len();
println!("{}", format!("Password length: {}", length));
if length >= 8 {
    println!("OK");
} else {
    println!("Too short");
}

STEP 1 OF 5
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Rust, explained

    Problem overview

    The first version of the Password Strength project looks at only one property, length. The program starts with let password = "Secret99";. It prints the length and a verdict: OK if the length is at least 8, Too short otherwise.

    Password length: 8
    OK
    

    For abc the output is Password length: 3 followed by Too short.

    Approach: measure once, use twice

    The length is needed twice: it goes into the first output line, and it feeds the comparison. So it is stored in a variable, length, and both uses read that variable. The verdict is a plain if/else on length >= 8. The comparison is "at least", so exactly 8 is acceptable.

    Step by step

    1. Create password with the text Secret99 in double quotes.
    2. Create length from password.len().
    3. Print the first line with println!. Here the text is built with format! and printed through "{}", which gives the same output as putting the label and the number straight into println!.
    4. Write the if for length >= 8 and print OK inside it.
    5. Add the else on the same line as the closing brace of the if, and print Too short inside it.

    The solution in Rust

            let password = "Secret99";
            let length = password.len();
            println!("{}", format!("Password length: {}", length));
            if length >= 8 {
                println!("OK");
            } else {
                println!("Too short");
            }
    

    Rust details worth knowing:

    • len() counts bytes, not characters. For plain ASCII text such as Secret99 the two are the same. For other text they are not: pässwor has 7 characters, but because ä takes two bytes in UTF-8, len() returns 8 and the check says OK. To count characters, use password.chars().count().
    • len() returns a usize. The literal 8 in length >= 8 takes the same type from the comparison, so no conversion is needed.
    • String literals are &str. password borrows text that lives in the program, and len() is a method on it.
    • format! returns a String. println!("{}", ...) prints that string and adds the newline.

    Bytes and characters

    The word "length" hides a choice. A password is text, and Rust stores text as UTF-8. Letters from the English alphabet take one byte each, so the byte count and the character count agree for Secret99. A letter such as ä takes two bytes. For the 7-character text pässwor, password.len() is 8 and password.chars().count() is 7.

    In this lesson the byte count is what the code uses. Both methods are cheap on short text, but they answer different questions: len() reads a stored number, while chars().count() walks through the text to count its characters.

    The shape to remember

    This version has three parts: some data, a measurement stored in a variable and a decision based on it. Later versions keep that shape and replace the single measurement with more of them. The next version builds on this one.

    Edge cases and common mistakes

    • Exactly 8. abcdefgh prints Password length: 8 and OK. Using > instead of >= would reject it.
    • One short. abcdefg has 7 characters and prints Too short.
    • Empty password. The length is 0 and the verdict is Too short.
    • Non-ASCII text. As noted above, len() counts bytes, so accented letters count twice.
    • Length says nothing about quality. aaaaaaaa passes this check. Later versions of the project add more rules.

    Testing

    PasswordOutput
    Secret99Password length: 8, OK
    abcPassword length: 3, Too short
    abcdefgPassword length: 7, Too short
    abcdefghPassword length: 8, OK
    (empty)Password length: 0, Too short
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free