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
- Create
passwordwith the textSecret!99. - Create
lengthfrompassword.len(). - Print the
Lengthline. - Create
has_upperandhas_lowerwithlet mut, bothfalse. - Create
has_digitandhas_symbolthe same way. - Start the loop with
for character in password.chars(). - Uppercase test:
character.is_ascii_uppercase()setshas_upper. - Lowercase test:
character.is_ascii_lowercase()setshas_lower. - Digit test:
character.is_ascii_digit()setshas_digit. - Symbol test:
!character.is_ascii_alphanumeric()setshas_symbol.is_ascii_alphanumeric()is true for letters and digits, so negating it leaves everything else. - After the loop, print the four flag lines in the order Upper, Lower, Digit, Symbol, each using the
ifthat gives"True"or"False". - Write the verdict: five conditions joined with
&&: the length and the four flags. - Add the
elsethat printsWeak.
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_upperis already abool, so&& has_upperis right and== truewould be redundant. - The
is_ascii_*methods are ASCII only. They look at the English lettersAtoZandatozand the digits0to9. A letter such aséis none of them, so it counts as a symbol here:Sécret99printsSymbol: Trueand isStrong. - The
!operator negates abool. 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 99has no!, yetSymbol: 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.
Secret99has everything but a symbol, so it isWeak. - Long enough but incomplete.
secret99has length 8 and still fails because of the upper and symbol flags. - All classes but too short.
Sec!99has all four flags true, but its length is 6, so it isWeak. - Symbol test written without
!. Testingis_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
Falseand the verdict isWeak.
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
| Password | Length | Flags (Upper, Lower, Digit, Symbol) | Verdict |
|---|---|---|---|
Secret!99 | 9 | True, True, True, True | Strong |
secret99 | 8 | False, True, True, False | Weak |
Secret99 | 8 | True, True, True, False | Weak |
Sec!99 | 6 | True, True, True, True | Weak |
Secret 99 | 9 | True, True, True, True | Strong |
| (empty) | 0 | False, False, False, False | Weak |
