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
- Create
passwordwith the textSecret99in double quotes. - Create
lengthfrompassword.len(). - Print the first line with
println!. Here the text is built withformat!and printed through"{}", which gives the same output as putting the label and the number straight intoprintln!. - Write the
ifforlength >= 8and printOKinside it. - Add the
elseon the same line as the closing brace of theif, and printToo shortinside 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 asSecret99the two are the same. For other text they are not:pässworhas 7 characters, but becauseätakes two bytes in UTF-8,len()returns 8 and the check saysOK. To count characters, usepassword.chars().count().len()returns ausize. The literal 8 inlength >= 8takes the same type from the comparison, so no conversion is needed.- String literals are
&str.passwordborrows text that lives in the program, andlen()is a method on it. format!returns aString.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.
abcdefghprintsPassword length: 8andOK. Using>instead of>=would reject it. - One short.
abcdefghas 7 characters and printsToo 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.
aaaaaaaapasses this check. Later versions of the project add more rules.
Testing
| Password | Output |
|---|---|
Secret99 | Password length: 8, OK |
abc | Password length: 3, Too short |
abcdefg | Password length: 7, Too short |
abcdefgh | Password length: 8, OK |
| (empty) | Password length: 0, Too short |
