Recap - Generic Printer
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 44 of 61.
Challenge
EasyLet'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 publicPrintabletrait with aprint_infomethod that takes&selfand returns aString. Then create a public generic function calledannouncethat accepts any typeTimplementingPrintable. This function should printAnnouncement: {info}where{info}is the result of callingprint_info()on the item.main.rs: Create two public structs that implementPrintable:Person— with a publicnamefield (String). Itsprint_infoshould returnPerson: {name}Product— with publicname(String) andprice(f64) fields. Itsprint_infoshould returnProduct: {name} - ${price}
announcewith each to demonstrate that your generic function works with anyPrintabletype.
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.99You 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
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