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
- Create
passwordwith the textSecret99. - Create
lengthfrompassword.len(). - Print the
Lengthline withprintln!; each{}in its text is filled by a value listed after it. - Create
has_digitwithlet mut, starting atfalse. - Loop with
for character in password.chars(), which hands out the characters one at a time. - Test each character with
character.is_ascii_digit()and sethas_digittotruewhen it passes. - Print the
Has digitline after the loop. Aboolprints as lowercasetrue, so the text is chosen withif has_digit { "True" } else { "False" }. - Write the verdict:
if length >= 8 && has_digitprintsStrong. - Add the
elsethat printsWeak. 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:
ifis an expression.if has_digit { "True" } else { "False" }produces a value, so it can sit insideprintln!as an argument. Both branches must have the same type, here&str, and neither ends with a semicolon.- Booleans print in lowercase.
println!("{}", true)printstrue, which is why the lesson picks its own text. chars()yieldscharvalues (Unicode characters), one per loop pass, whilelen()counts bytes.is_ascii_digit()accepts only0to9. A digit from another script, such as the Arabic-Indic٣, givesfalse.&&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
elsethat setshas_digitback tofalsewould keep only the last character's answer. - Printing before the loop ends. The
Has digitline belongs after the loop, so it runs once with the final value. - Digits only.
12345678has length 8 and a digit, so it isStrong. The rules ask for nothing more. - Short with a digit.
a1has a digit but length 2, so it isWeak. - Empty password. The loop never runs; the output is
Length: 0,Has digit: False,Weak. - Using
||instead of&&. That would callabcdefghstrong, which fails the digit rule.
Testing
| Password | Has digit line | Verdict |
|---|---|---|
Secret99 | Has digit: True | Strong |
abcdefgh | Has digit: False | Weak |
a1 | Has digit: True | Weak |
12345678 | Has digit: True | Strong |
| (empty) | Has digit: False | Weak |
