Problem overview
Version 5 rejects well-known passwords. The program starts with a set of four common ones, password, 123456, qwerty and letmein, and the password to check, qwerty. It prints the password, whether it is common and a verdict:
Password: qwerty
Common: True
Weak: reject
For Tr0ub4dor, which is not in the set, the output is Password: Tr0ub4dor, Common: False and Strong: accept.
Approach: ask a set
A list of common passwords could be searched one entry at a time. A HashSet answers "is this value in here?" directly, without looking at each entry, and the answer stays fast however many passwords the set holds. The set has no values attached to its entries; it only remembers which ones are present, which is exactly what this check needs.
The program stores the answer in a boolean, is_common, because it is used twice: for the Common line and for the verdict.
Step by step
- Create
commonwithHashSet::from([...]), passing the four passwords in an array. - Create
passwordwith the text"qwerty". - Print the
Passwordline. - Create
is_commonfromcommon.contains(password), which is true when the set holds that text. - Print the
Commonline, choosing"True"or"False"with anifexpression, because Rust prints a bool in lowercase. - Write the
ifonis_commonand printWeak: reject. - Add the
elsethat printsStrong: accept.
The solution in Rust
let common = HashSet::from(["password", "123456", "qwerty", "letmein"]);
let password = "qwerty";
println!("Password: {}", password);
let is_common = common.contains(password);
println!("Common: {}", if is_common { "True" } else { "False" });
if is_common {
println!("Weak: reject");
} else {
println!("Strong: accept");
}
Rust details worth knowing:
- Where
HashSetcomes from. It is instd::collections, so a program brings it into scope withuse std::collections::HashSet;. HashSet::fromtakes an array. The set's element type,&strhere, is inferred from the strings.containstakes a reference.passwordis already a&str, so it is passed as is.- A bool can be a condition directly.
if is_commonneeds no== true. - The verdict is not tied to length or classes. A common password is rejected whatever else is true of it, and a password that is not common is accepted here without any other check.
Sets and lists
A set stores each value once. Inserting the same text twice leaves one copy: the first insert of "a" returns true, the second returns false, and the set's length stays 1. That matches the job here, because a password is either on the list or not, and a duplicate entry would add nothing.
A Vec of the same four strings would also answer the question with contains, but it compares the password with the entries one by one. The set computes a hash of the text and goes straight to where it would be. For four entries the difference is invisible. For a list of a million common passwords it matters.
Edge cases and common mistakes
- Case matters.
Qwertyis not in the set, so it printsCommon: FalseandStrong: accept. - Whitespace matters.
qwertywith a trailing space does not match either. - Digits-only entries.
123456is in the set as text, so it is rejected. - Empty password. It is not in the set, so this version accepts it.
- Printing the bool directly.
println!("Common: {}", is_common)printstruein lowercase, which does not match the expected text. - Comparing against a list by hand. A chain of
==tests works for four passwords but grows with every new entry; the set does not.
Testing
| Password | Common line | Verdict |
|---|---|---|
qwerty | Common: True | Weak: reject |
Tr0ub4dor | Common: False | Strong: accept |
Qwerty | Common: False | Strong: accept |
123456 | Common: True | Weak: reject |
| (empty) | Common: False | Strong: accept |
