Menu
Coddy logo textTech

The Public Keyword

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

In the previous lesson, you learned how to organize code into separate module files. However, if you tried to use the Player struct from main.rs, you'd encounter a compiler error. That's because everything in a Rust module is private by default.

To make items accessible from outside their module, you must explicitly mark them with the pub keyword:

// player.rs
pub struct Player {
    pub name: String,
    pub score: u32,
}

impl Player {
    pub fn new(name: String) -> Player {
        Player { name, score: 0 }
    }
}

Notice how pub appears in multiple places—on the struct itself, on each field, and on the method. Each item needs its own visibility declaration.

Without pub on the struct, other files can't even name the type. Without pub on fields, they can't access the data directly. Without pub on methods, they can't call them.

Now in main.rs, you can use the module's public items:

// main.rs
mod player;

use player::Player;

fn main() {
    let p = Player::new(String::from("Alice"));
    println!("{}", p.name);
}

The use statement brings Player into scope so you don't have to write player::Player every time. This privacy-by-default approach is intentional—it encourages you to think carefully about what parts of your code should be exposed to the outside world.

challenge icon

Challenge

Easy

Let's build a simple product catalog by organizing your code across two files and making the right items public so they can be used from your main program.

You'll create two files:

  • product.rs: Define a Product struct with two fields: name (String) and price (f64). Add an associated function called new that takes a name and price and returns a new Product. Remember to mark the struct, its fields, and the method as public so they can be accessed from outside the module.
  • main.rs: Declare the product module, bring Product into scope with a use statement, then create a product and print its details.

Think carefully about where you need the pub keyword—the struct itself needs to be visible, the fields need to be accessible for reading, and the constructor method needs to be callable from main.rs.

Your output should follow this exact format:

Product: {name}, Price: ${price}

For example, if you create a product with name "Laptop" and price 999.99, the output would be:

Product: Laptop, Price: $999.99

You will receive two inputs: the product name and the price.

Cheat sheet

In Rust, everything in a module is private by default. To make items accessible from outside their module, use the pub keyword.

Mark structs, fields, and methods as public:

// player.rs
pub struct Player {
    pub name: String,
    pub score: u32,
}

impl Player {
    pub fn new(name: String) -> Player {
        Player { name, score: 0 }
    }
}

Each item needs its own visibility declaration:

  • pub on the struct makes the type visible
  • pub on fields allows direct access to data
  • pub on methods allows calling them

Use the use statement to bring items into scope:

// main.rs
mod player;

use player::Player;

fn main() {
    let p = Player::new(String::from("Alice"));
    println!("{}", p.name);
}

Try it yourself

// Declare the product module
mod product;

// TODO: Bring Product into scope with a use statement

fn main() {
    // Read input
    let mut name = String::new();
    std::io::stdin().read_line(&mut name).expect("Failed to read line");
    let name = name.trim().to_string();
    
    let mut price_input = String::new();
    std::io::stdin().read_line(&mut price_input).expect("Failed to read line");
    let price: f64 = price_input.trim().parse().expect("Failed to parse price");
    
    // TODO: Create a new Product using the Product::new function
    
    // TODO: Print the product details in the format:
    // Product: {name}, Price: ${price}
}
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