Mutable Structs
Part of the Logic & Flow section of Coddy's Rust journey — lesson 23 of 66.
By default, struct instances in Rust are immutable - once you create them, you cannot change their field values. However, just like with other variables, you can make a struct instance mutable by adding the mut keyword when declaring it.
When you declare a struct instance as mutable, you can modify any of its fields using dot notation:
let mut player = Player {
name: String::from("Alice"),
score: 0,
};
player.score = 100; // This works because player is mutableWithout the mut keyword, attempting to change a field would result in a compilation error. The mutability applies to the entire struct instance - you cannot make individual fields mutable while keeping others immutable.
Challenge
EasyYou will receive four inputs: a player name, an initial score (as an integer), a bonus score to add (as an integer), and a final score adjustment (as an integer). Define a Player struct with two fields: name of type String and score of type i32. Create a mutable instance of this struct, then modify the score field multiple times using the input values.
Requirements:
- Define a
Playerstruct with fields:name: Stringandscore: i32 - Read the first input as the player name
- Read the second input and convert it to
i32for the initial score - Create a mutable instance of the
Playerstruct with these values - Read the third input and convert it to
i32for the bonus score - Add the bonus score to the player's current score using dot notation
- Read the fourth input and convert it to
i32for the final adjustment - Add the final adjustment to the player's current score
- Print the player information in the exact format shown below
Input:
- First line: Player name (e.g.,
Alice) - Second line: Initial score as an integer (e.g.,
50) - Third line: Bonus score as an integer (e.g.,
30) - Fourth line: Final adjustment as an integer (e.g.,
20)
Output:
- First line:
Player: [name] - Second line:
Final Score: [score]
Cheat sheet
Struct instances in Rust are immutable by default. To modify field values after creation, declare the instance as mutable using the mut keyword:
let mut player = Player {
name: String::from("Alice"),
score: 0,
};
player.score = 100; // Modify field using dot notationMutability applies to the entire struct instance - you cannot make individual fields mutable while keeping others immutable.
Try it yourself
use std::io;
// TODO: Define the Player struct here with name and score fields
fn main() {
// Read player name
let mut name = String::new();
io::stdin().read_line(&mut name).expect("Failed to read line");
let name = name.trim().to_string();
// Read initial score
let mut initial_score = String::new();
io::stdin().read_line(&mut initial_score).expect("Failed to read line");
let initial_score: i32 = initial_score.trim().parse().expect("Invalid number");
// Read bonus score
let mut bonus = String::new();
io::stdin().read_line(&mut bonus).expect("Failed to read line");
let bonus: i32 = bonus.trim().parse().expect("Invalid number");
// Read final adjustment
let mut adjustment = String::new();
io::stdin().read_line(&mut adjustment).expect("Failed to read line");
let adjustment: i32 = adjustment.trim().parse().expect("Invalid number");
// TODO: Create a mutable Player instance
// TODO: Add the bonus score to the player's score
// TODO: Add the final adjustment to the player's score
// TODO: Print the output in the required format
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together