Menu
Coddy logo textTech

Recap - Secure Locker

Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 13 of 61.

challenge icon

Challenge

Easy

Let'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 a Locker struct with two private fields: code (u32) and contents (String). The struct should be public, but both fields must remain private to protect the locker's security. Implement:
    • A new constructor that takes an initial code and contents
    • A set_code method that allows changing the locker's code
    • A get_contents method that takes an attempted code—if it matches the locker's code, return the contents; otherwise, return "Access denied"
  • 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 coins

You 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