Final Interaction
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 23 of 61.
Challenge
EasyCongratulations on building your Virtual Pet! In this final step, you'll bring everything together by simulating a complete interaction session with your pet. This is where all your methods work in harmony!
Your pet module already has everything it needs: feeding, playing, and status reporting. Now you'll create a simulation that shows a day in the life of your virtual companion.
In your main.rs, you'll read your pet's name and then perform a sequence of interactions:
- Create a new pet with the given name
- Display the initial status report
- Play with your pet twice
- Feed your pet once
- Display the final status report
Between the two status reports, print a separator line to show the transition:
--- After Activities ---For example, if the input is:
BuddyThe output should be:
--- Status Report ---
Name: Buddy
Energy: 100
Hunger: 0
Mood: happy
Buddy had fun playing!
Buddy had fun playing!
Buddy enjoyed the meal!
--- After Activities ---
--- Status Report ---
Name: Buddy
Energy: 60
Hunger: 0
Mood: happyRemember how the stats change: playing costs 20 energy and adds 15 hunger each time, while feeding reduces hunger by 30. After two play sessions and one feeding, your pet's energy drops from 100 to 60, and hunger goes from 0 to 30 then back to 0.
This final challenge showcases the complete Virtual Pet system you've built—a fully encapsulated module with private fields, getters, setters, and methods that work together to create an interactive experience!
Try it yourself
mod pet;
use pet::Pet;
fn main() {
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 energy_input = String::new();
std::io::stdin().read_line(&mut energy_input).expect("Failed to read line");
let initial_energy: u32 = energy_input.trim().parse().expect("Failed to parse energy");
let mut hunger_input = String::new();
std::io::stdin().read_line(&mut hunger_input).expect("Failed to read line");
let initial_hunger: u32 = hunger_input.trim().parse().expect("Failed to parse hunger");
let mut my_pet = Pet::new(name);
my_pet.set_energy(initial_energy);
my_pet.set_hunger(initial_hunger);
my_pet.status();
}
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