Problem overview
Version 6 combines the character checks into one number. The score starts at 0 and gains points for each property of the password:
- 25 for a length of at least 8;
- 20 for an uppercase letter;
- 20 for a lowercase letter;
- 20 for a digit;
- 15 for a symbol, meaning a character that is neither a letter nor a digit.
The maximum is 25 + 20 + 20 + 20 + 15 = 100. For Secret!99 the program prints:
Length: 9
Score: 100
For abc123 it prints Length: 6 and Score: 40: a lowercase letter and a digit, 20 each, and nothing else.
Approach: collect facts, then add up points
The program separates two jobs. First a loop over the characters sets four flags: has an uppercase letter, has a lowercase letter, has a digit, has a symbol. Then a series of independent if statements adds each rule's points to score. Each rule is its own if, never an else if, because a password can earn several awards at once. Keeping the scan and the scoring apart makes each easy to change on its own.
Step by step
- Create
passwordwith the text"Secret!99". - Create
lengthfrompassword.len(). - Create the four flags,
hasUpper,hasLower,has_digitandhasSymbol, each withlet mutandfalse. - Loop with
for c in password.chars(). - Test uppercase with
c.is_uppercase(), and sethasUpper. - Test lowercase with
c.is_lowercase(), and sethasLower. - Test digit with
c.is_ascii_digit(), and sethas_digit. - Test symbol with
!c.is_alphanumeric(), and sethasSymbol. - Create
scorewithlet mut, set to 0. - Add 25 if
length >= 8. - Add 20 for each of
hasUpper,hasLowerandhas_digit, and add 15 forhasSymbol, each in its ownif. - Print the
Lengthline, then theScoreline.
The solution in Rust
let mut password = "Secret!99";
let mut length = password.len();
let mut hasUpper = false;
let mut hasLower = false;
let mut has_digit = false;
let mut hasSymbol = false;
for c in password.chars() {
if c.is_uppercase() {
hasUpper = true;
}
if c.is_lowercase() {
hasLower = true;
}
if c.is_ascii_digit() {
has_digit = true;
}
if !c.is_alphanumeric() {
hasSymbol = true;
}
}
let mut score = 0;
if length >= 8 {
score += 25;
}
if hasUpper {
score += 20;
}
if hasLower {
score += 20;
}
if has_digit {
score += 20;
}
if hasSymbol {
score += 15;
}
println!("Length: {}", length);
println!("Score: {}", score);
Rust details worth knowing:
- Unicode-aware tests. This version uses
is_uppercase(),is_lowercase()andis_alphanumeric(), which understand letters beyond ASCII. The digit test is stillis_ascii_digit(). SoÉcounts as uppercase, andéas a letter rather than a symbol. ForSécret!99the score is 100, with theécounted as a lowercase letter and the!as the only symbol. - Bytes versus characters.
len()counts bytes, soéadds 2 to the length.Sécret!99printsLength: 10although it has 9 characters. - The compiler's warnings. Names such as
hasUppercompile, butrustcwarnsvariable `hasUpper` should have a snake case nameand suggestshas_upper, the usual Rust style. Themutonpasswordandlengthis also unnecessary, because neither changes, andrustcsaysvariable does not need to be mutable. They are warnings only, so the program builds and runs. scoreis ani32. The literals 25, 20 and 15 add to it with+=, and 100 fits easily.- The flags are checked directly.
if hasUpperneeds no comparison.
Tracing the score
For abc123 the loop sets the lowercase and digit flags, so the four flags end as false, true, true and false. Then the rules run in order, and only the ones whose condition holds add points:
lower -> 20
digit -> 40
score 40
The length rule does not fire, because the length is 6, and neither do the uppercase and symbol rules. Each if is tested independently, so the order of the rules does not change the final sum.
The same score with any
The flags are only used to feed the points. Iterator methods can skip them:
let mut score = 0;
if password.len() >= 8 { score += 25; }
if password.chars().any(|c| c.is_uppercase()) { score += 20; }
if password.chars().any(|c| c.is_lowercase()) { score += 20; }
if password.chars().any(|c| c.is_ascii_digit()) { score += 20; }
if password.chars().any(|c| !c.is_alphanumeric()) { score += 15; }
For Secret!99 this also gives 100. It scans the text once per rule instead of once in total. The lesson's version, with one loop and four flags, shows the scan and the scoring as two separate jobs.
The compiler error for a missing mut
Forgetting mut is the most common slip in this kind of program. Declaring let score = 0; and then writing score += 25; in an if fails to compile, with this message:
error[E0384]: cannot assign twice to immutable variable `score`
The compiler's own suggestion is to write let mut score = 0;. The same applies to every counter and flag in this project: a variable that changes inside a loop needs mut, and one that never changes does not.
Why the weights add up to 100
The point values are a design choice, not a Rust rule: 25 for length, 20 for each of three character classes and 15 for symbols, so a password that passes everything lands on exactly 100. Because every rule is an independent if, changing a weight means editing one number, and adding a rule means adding one more if. The score can never go above 100 or below 0 here, since it only ever grows from 0 by those five amounts.
Edge cases and common mistakes
- Using
else iffor the rules. Only the first matching award would be added, and the score would stall. - Length alone.
12345678scores 45: 25 for length plus 20 for a digit.abcdefghandABCDEFGHalso score 45. - Short and varied.
abc123scores 40 despite being short, because the length points are the only ones it misses. - Empty password. The score is 0.
- Printing before scoring. The
Scoreline must come after theifstatements, or it shows 0. - Test choice.
is_ascii_digit()keeps digits to0to9;is_numeric()would also accept digits from other scripts.
Testing
| Password | Length | Score |
|---|---|---|
Secret!99 | 9 | 100 |
abc123 | 6 | 40 |
abcdefgh | 8 | 45 |
ABCDEFGH | 8 | 45 |
12345678 | 8 | 45 |
Sécret!99 | 10 | 100 |
| (empty) | 0 | 0 |
