Recap - Secure Locker
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 13 of 61.
Challenge
EasyLet's build a secure locker system that brings together everything you've learned about encapsulation! Your locker will protect its contents behind a secret code—only someone who knows the code can see what's inside.
You'll create two files to organize your code:
locker.rs: Define aLockerstruct with two private fields:code(u32) andcontents(String). The struct should be public, but both fields must remain private to protect the locker's security. Implement:- A
newconstructor that takes an initial code and contents - A
set_codemethod that allows changing the locker's code - A
get_contentsmethod that takes an attempted code—if it matches the locker's code, return the contents; otherwise, return"Access denied"
- A
main.rs: Bring in your locker module, create a locker, and demonstrate its security features by attempting to access the contents with both correct and incorrect codes.
The get_contents method should return a String—either the actual contents when the code is correct, or the denial message when it's wrong. This is your conditional getter that validates access before revealing private data.
Your output should follow this exact format:
Attempt with {code}: {result}
Code changed
Attempt with {code}: {result}For example, if you create a locker with code 1234 and contents "Gold coins", then try code 0000, change the code to 5678, and try 5678, the output would be:
Attempt with 0000: Access denied
Code changed
Attempt with 5678: Gold coinsYou will receive five inputs: the initial code, the contents, the first attempt code, the new code to set, and the second attempt code.
Try it yourself
mod locker;
use locker::Locker;
fn main() {
// Read inputs
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
let initial_code: u32 = input.trim().parse().expect("Invalid number");
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
let contents = input.trim().to_string();
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
let first_attempt: u32 = input.trim().parse().expect("Invalid number");
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
let new_code: u32 = input.trim().parse().expect("Invalid number");
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
let second_attempt: u32 = input.trim().parse().expect("Invalid number");
// TODO: Create a new Locker with initial_code and contents
// TODO: Attempt to get contents with first_attempt code
// Print: "Attempt with {code}: {result}"
// TODO: Change the locker code to new_code
// Print: "Code changed"
// TODO: Attempt to get contents with second_attempt code
// Print: "Attempt with {code}: {result}"
}
All lessons in Object Oriented Programming
1Methods and Behavior
Intro Implementation BlocksThe Self ParameterMutable MethodsAssociated FunctionsMultiple Implementation BlocksMethod ChainingRecap - Rectangle Actions4Project: Virtual Pet
Defining the PetFeeding the Pet7Standard Traits
The Debug TraitThe Display TraitClone and CopyEquality TraitsRecap - Printable Point10Project: Document System
The Draw TraitText Component2Encapsulation and Modules
Modules BasicsThe Public KeywordPrivate FieldsGettersSettersRecap - Secure Locker5Generics
Generic StructsGeneric MethodsMultiple Generic TypesGeneric FunctionsRecap - Coordinate Point8Traits as Bounds
Trait Bounds SyntaxMultiple BoundsThe Where ClauseReturning Types with TraitsRecap - Generic Printer11Design Patterns in Rust
Newtype PatternCompositionThe Drop TraitFrom and IntoRecap - Smart Pointer Mock