Activity 20: Design Your Own Structs and Methods
Overview
In groups of 5-6, you'll design a struct-based system for a real-world scenario. Focus on:
- What fields belong in your structs
- What enums represent choices in your domain
- What methods you need and what type of
selfparameter each uses - How structs and enums work together
Time:
- 10 minutes: Group design work
- 10 minutes: Group presentations
Groups 1+2: Front of the room, Task A Groups 3+4: In the seats, Task B Groups 5+6: Outside the doors, Task C Groups 7+8: Lobby area, Task D
Instructions
- Add everyone's name to the sheet
- Design your system on paper:
- List the main struct(s) you need with their fields and types
- List any enums that represent choices or states
- Write any
implblocks you need and method signatures (including whether it takesself,&self, or&mut self) - but feel free to leave the inside of each methodunimplemented()
- Be ready to present:
- Choose one person who will come to the front to explain your design
- We'll go by task, so we'll hear two approaches to each problem
Task A: Smart Home Thermostat & Lights
Design a smart home system with thermostats and light bulbs.
Consider:
- A
Thermostatstruct - what fields does it need?- Current temperature? Target temperature? Current mode?
- A
SmartLightstruct - what makes a smart light?- Brightness level? Color? On/off state?
- What enums represent choices?
- Thermostat modes (Heat/Cool/Auto/Off)?
- Light colors or temperature?
Methods to think about:
- How do you read the current temperature? (
&self?) - How do you change the target temperature? (
&mut self?) - How do you turn a light on/off? (
&mut self?) - What about a factory reset that returns the device to defaults? (
self?)
Bonus: How might you store multiple lights in a home?
Task B: Coffee Shop Order System
Design a system for taking and managing coffee shop orders.
Consider:
- An
Orderstruct - what's in an order?- Customer name?
- Items ordered?
- Total price?
- Order status?
- A
Drinkstruct - what defines a drink?- Type (latte, cappuccino, etc.)?
- Size?
- Customizations (extra shot, oat milk, etc.)?
- What enums would help?
- DrinkSize (Small/Medium/Large)?
- OrderStatus (Pending/InProgress/Ready/Completed)?
- MilkType (Whole/Skim/Oat/Almond)?
Methods to think about:
- How do you calculate the total price? (
&self?) - How do you add an item to an order? (
&mut self?) - How do you mark an order as ready? (
&mut self?) - What about completing/closing an order so it can't be modified? (
selfto consume it?)
Bonus: How do you handle customizations? Separate struct? Enum? Vec of options?
Task C: Music Streaming Playlist
Design a music streaming app's playlist system.
Consider:
- A
Playliststruct - what data does a playlist have?- Name, creator?
- List of songs?
- Play count? Duration?
- Public or private?
- A
Songstruct - what identifies a song?- Title, artist, album?
- Duration in seconds?
- Genre?
- What enums fit?
- Genre (Rock/Pop/Jazz/Classical/...)?
- PlaylistVisibility (Public/Private/Unlisted)?
Methods to think about:
- How do you get the total playlist duration? (
&self?) - How do you add/remove songs from a playlist? (
&mut self?) - How do you shuffle the playlist? (
&mut self?) - What about converting a playlist to a "mix" that can't be edited? (
selfto consume?)
Bonus: Should shuffle return a new playlist or modify the existing one?
Task D: RPG Game Character
Design a role-playing game character system.
Consider:
- A
Characterstruct - what defines a character?- Name, level?
- Health points (current and max)?
- Inventory of items?
- Character class?
- An
Itemstruct - what's in the inventory?- Name, description?
- Item type (weapon, armor, potion)?
- Value or power?
- What enums make sense?
- CharacterClass (Warrior/Mage/Rogue/Healer)?
- ItemType (Weapon/Armor/Potion/Quest)?
Methods to think about:
- How do you check if a character is alive? (
&self?) - How do you take damage or heal? (
&mut self?) - How do you level up? (
&mut self?) - What about "retiring" a character and getting their final stats? (
selfto consume?)
Bonus: How does a character "use" an item from inventory?
Presentation Guidelines
When presenting (2 minutes per group):
-
Introduce your scenario (15 seconds)
- "We designed a [system name]"
-
Show your main struct(s) (45 seconds)
- "Our main struct is [Name] with fields: ..."
- "We also have [other structs]"
-
Show your enums (30 seconds)
- "We used enums for [choices]: ..."
-
Highlight interesting method (30 seconds)
- Pick ONE interesting method
- Explain why you chose
&self,&mut self, orself - "We made X take &mut self because it needs to change..."
Discussion Questions (After Presentations)
- Which groups had similar design decisions?
- Did anyone use
self(consuming) methods? When and why? - What made you choose an enum vs adding a field to a struct?
- Did any groups nest structs inside other structs?
- How did you decide what should be a separate struct vs just a field?
Key Concepts
As you work, remember:
-
&self: Use when you just need to READ data- Getting values, calculations, checking status
- The struct is still usable afterward
-
&mut self: Use when you need to CHANGE data- Updating fields, adding to collections, state changes
- The struct is still usable afterward
-
self: Use when you CONSUME the struct- Converting to something else, finalizing, deleting
- The struct is NOT usable afterward
-
Enums: Use for CHOICES (one of several alternatives)
- "This OR that" relationships
- Statuses, modes, types
-
Structs: Use for GROUPING related data
- "This AND that" relationships
- Data that belongs together