You build the struct Phone. It keeps a secret PIN and counts the wrong tries.
Both fields are private: they have no pub. Other modules can't read or change them. They can only call the methods below.
new(pin)returns a phone with that PIN and no wrong tries.is_locked()returnstrueafter 3 wrong tries. Before that, it returnsfalse.unlock(attempt)returnstruewhenattemptis the PIN.- Any other attempt returns
false. It counts as one more wrong try. - A locked phone stays locked for good. Then
unlockalways returnsfalse, even with the right PIN. - The right PIN doesn't reset the count of wrong tries.
The method unlock changes the count. So it takes &mut self. The method is_locked only reads. So it takes &self. The starter code has these headers.
Each test case makes one phone with the given PIN. Then it calls the methods in order. The output line shows each answer as True or False.
Example 1:
Input:
pin = "1234"
unlock("1111")
unlock("1234")
is_locked()
Output: False True False
Explanation: "1111" is wrong. That is 1 wrong try.
"1234" is the PIN, so the phone opens.
1 wrong try doesn't lock the phone.
Example 2:
Input:
pin = "0042"
unlock("4200")
unlock("0000")
unlock("0041")
is_locked()
unlock("0042")
Output: False False False True False
Explanation: Three wrong tries lock the phone.
After that, even the right PIN "0042" gets False.
Example 3:
Input:
pin = "7777"
unlock("1234")
unlock("7777")
unlock("0000")
unlock("7777")
unlock("9999")
is_locked()
Output: False True False True False True
Explanation: The right PIN works twice.
It doesn't reset the count.
"9999" is the third wrong try, so the phone locks.
Constraints:
- Each PIN has exactly 4 digits.
- Each attempt has exactly 4 digits too.
- A PIN can start with 0, like "0042".
- 1 to 1000 calls per phone.
Free plan: 30 tutor messages. Premium is unlimited.
