Start learning free

LESSON 171 OF 414 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 - Length Check in Rust

Practice

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

    Password Strength V1 in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free