Menu
Coddy logo textTech

Getters

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

With private fields, external code can't access your struct's data directly. But sometimes you need to let others read the data without giving them the ability to change it. This is where getter methods come in.

A getter is simply a public method that returns a reference to a private field:

// wallet.rs
pub struct Wallet {
    balance: f64,
}

impl Wallet {
    pub fn new(initial: f64) -> Wallet {
        Wallet { balance: initial }
    }
    
    pub fn balance(&self) -> f64 {
        self.balance
    }
}

The balance() method takes &self (read-only access) and returns the value of the private field. In Rust, getters are typically named after the field itself—no get_ prefix needed.

Now external code can read the balance through the method:

// main.rs
mod wallet;
use wallet::Wallet;

fn main() {
    let w = Wallet::new(100.0);
    println!("Balance: {}", w.balance());  // Works!
}

For fields containing owned data like String, you'll often return a reference to avoid unnecessary copying:

pub fn name(&self) -> &String {
    &self.name
}

Getters give you controlled read access while keeping the field itself protected. The struct remains in charge of its data—external code can look, but can't touch.

challenge icon

Challenge

Easy

Let's build a user profile system that demonstrates how getter methods provide controlled read access to private data.

You'll create two files to organize your code:

  • profile.rs: Define a UserProfile struct with two private fields: username (String) and age (u32). The struct itself should be public, but the fields should remain private. Implement a public new constructor that takes a username and age. Then add two getter methods:
    • username — returns a reference to the username (&String)
    • age — returns the age value (u32)
  • main.rs: Bring in your profile module, create a UserProfile, and use the getter methods to print the user's information.

Remember that getters in Rust are typically named after the field itself—no get_ prefix needed. For String fields, return a reference to avoid unnecessary copying.

Your output should follow this exact format:

Username: {username}
Age: {age}

For example, if you create a profile with username "alice" and age 25, the output would be:

Username: alice
Age: 25

You will receive two inputs: the username and the age.

Cheat sheet

A getter method is a public method that provides read-only access to a private field.

Getters allow external code to view data without being able to modify it directly:

pub struct Wallet {
    balance: f64,
}

impl Wallet {
    pub fn new(initial: f64) -> Wallet {
        Wallet { balance: initial }
    }
    
    pub fn balance(&self) -> f64 {
        self.balance
    }
}

In Rust, getters are named after the field itself—no get_ prefix is needed.

For fields containing owned data like String, return a reference to avoid unnecessary copying:

pub fn name(&self) -> &String {
    &self.name
}

Using a getter method:

let w = Wallet::new(100.0);
println!("Balance: {}", w.balance());

Try it yourself

mod profile;

use profile::UserProfile;

fn main() {
    let mut input1 = String::new();
    std::io::stdin().read_line(&mut input1).expect("Failed to read line");
    let username = input1.trim().to_string();

    let mut input2 = String::new();
    std::io::stdin().read_line(&mut input2).expect("Failed to read line");
    let age: u32 = input2.trim().parse().expect("Failed to parse age");

    // TODO: Create a UserProfile using the constructor

    // TODO: Use the getter methods to print the username and age
    // Format:
    // Username: {username}
    // Age: {age}
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming