Menu
Coddy logo textTech

Recap - Generic Printer

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

challenge icon

Challenge

Easy

Let's build a flexible announcement system that demonstrates the power of trait bounds! You'll create a Printable trait and a generic function that can announce any type implementing this trait—showing how generics and traits work together to create reusable, type-safe code.

You'll organize your code across two files:

  • printable.rs: Define a public Printable trait with a print_info method that takes &self and returns a String. Then create a public generic function called announce that accepts any type T implementing Printable. This function should print Announcement: {info} where {info} is the result of calling print_info() on the item.
  • main.rs: Create two public structs that implement Printable:
    • Person — with a public name field (String). Its print_info should return Person: {name}
    • Product — with public name (String) and price (f64) fields. Its print_info should return Product: {name} - ${price}
    Bring in your printable module, use the inputs to create a Person and a Product, then call announce with each to demonstrate that your generic function works with any Printable type.

The beauty of this design is that announce doesn't care whether it receives a Person, Product, or any future type—it only requires that the type can provide information through print_info(). The trait bound guarantees this at compile time.

Your output should show both announcements:

Announcement: Person: {name}
Announcement: Product: {product_name} - ${price}

For example, with inputs Alice, Laptop, and 999.99:

Announcement: Person: Alice
Announcement: Product: Laptop - $999.99

You will receive three inputs: the person's name, the product name, and the product price (parse as f64).

Try it yourself

mod printable;

use printable::{Printable, announce};

// TODO: Define a public struct Person with a public name field (String)
// Implement the Printable trait for Person
// print_info should return "Person: {name}"

// TODO: Define a public struct Product with public name (String) and price (f64) fields
// Implement the Printable trait for Product
// print_info should return "Product: {name} - ${price}"

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

    let mut product_name = String::new();
    std::io::stdin().read_line(&mut product_name).expect("Failed to read line");
    let product_name = product_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 Person instance with person_name

    // TODO: Create a Product instance with product_name and price

    // TODO: Call announce with the Person

    // TODO: Call announce with the Product
}

All lessons in Object Oriented Programming