📖 Scenario: You are creating a simple program to manage user profiles in a system. Each user has a name, age, and email. Sometimes, you want to create a new user based on an existing user but with some changes.
🎯 Goal: Learn how to use Rust's struct update syntax to create a new struct instance by copying fields from an existing instance while changing some fields.
📋 What You'll Learn
Create a struct called User with fields name, age, and email
Create an instance of User called user1 with specific values
Create a new instance user2 using struct update syntax to copy from user1 but change the email
Print the details of user2 to show the updated email and copied fields
💡 Why This Matters
🌍 Real World
Struct update syntax is useful when you want to create a new data record based on an existing one but with some changes, such as updating user profiles or configuration settings.
💼 Career
Understanding struct update syntax is important for Rust developers working on applications that manage data efficiently and safely, such as web servers, system tools, or game development.
Progress0 / 4 steps
1
Define the User struct and create user1
Define a struct called User with fields name of type String, age of type u32, and email of type String. Then create a variable called user1 of type User with name set to "Alice", age set to 30, and email set to "alice@example.com".
Rust
Hint
Use struct User { name: String, age: u32, email: String } to define the struct. Use String::from("Alice") to create string values.
2
Create a new email variable for the updated user
Create a variable called new_email and set it to String::from("alice.new@example.com").
Rust
Hint
Use let new_email = String::from("alice.new@example.com") to create the new email string.
3
Create user2 using struct update syntax
Create a variable called user2 of type User. Set its email field to new_email and use struct update syntax to copy the remaining fields from user1.
Rust
Hint
Use let user2 = User { email: new_email, ..user1 }; to copy fields from user1 but update email.
4
Print the details of user2
Add println! statements to print user2.name, user2.age, and user2.email each on its own line.
Rust
Hint
Use println!("{}", user2.name); and similarly for age and email.
Practice
(1/5)
1. What does the Rust struct update syntax ..old_struct do when creating a new struct?
easy
A. Copies all remaining fields from old_struct to the new struct
B. Deletes the old_struct after creating the new struct
C. Moves all fields from old_struct without copying
D. Creates a reference to old_struct instead of a new struct
Solution
Step 1: Understand struct update syntax
The syntax ..old_struct copies all fields from old_struct that are not explicitly set in the new struct.
Step 2: Effect on new struct creation
This lets you create a new struct with some fields changed, while reusing the rest from old_struct.
Final Answer:
Copies all remaining fields from old_struct to the new struct -> Option A
2. Which of the following is the correct way to use struct update syntax in Rust?
easy
A. let new_user = User { name: "Alice"; ..old_user };
B. let new_user = User { name: "Alice", ..old_user };
C. let new_user = User { name: "Alice" ..old_user };
D. let new_user = User { ..old_user, name: "Alice" };
Solution
Step 1: Check correct syntax order
In Rust, fields you want to change come first, then ..old_struct copies the rest.
Step 2: Identify correct punctuation and placement
let new_user = User { name: "Alice", ..old_user }; correctly places ..old_user after the changed field with commas separating fields.
Final Answer:
let new_user = User { name: "Alice", ..old_user }; -> Option B
Quick Check:
Changed fields first, then ..old_struct [OK]
Hint: Put changed fields before ..old_struct [OK]
Common Mistakes:
Placing ..old_struct before changed fields
Missing commas between fields
Using semicolons inside struct initialization
3. Given the code:
struct Point { x: i32, y: i32 }
let p1 = Point { x: 5, y: 10 };
let p2 = Point { x: 8, ..p1 };
println!("({}, {})", p2.x, p2.y);
What is the output?
medium
A. (8, 0)
B. (5, 10)
C. Compilation error
D. (8, 10)
Solution
Step 1: Analyze struct initialization for p2
p2 sets x to 8 explicitly, and copies y from p1 (which is 10) using struct update syntax.
Step 2: Determine printed values
Printing p2.x and p2.y outputs (8, 10).
Final Answer:
(8, 10) -> Option D
Quick Check:
Changed x=8, copied y=10 [OK]
Hint: Changed fields override, others copy from old struct [OK]
Common Mistakes:
Assuming y resets to 0
Thinking x stays 5
Expecting a compile error
4. What is wrong with this Rust code?
struct User { name: String, age: u32 }
let old_user = User { name: String::from("Bob"), age: 30 };
let new_user = User { age: 25, ..old_user };
println!("{} is {} years old", new_user.name, new_user.age);
medium
A. It causes a syntax error due to missing commas
B. It causes a move error because old_user is partially moved
C. It compiles and runs without errors
D. It prints wrong age value
Solution
Step 1: Understand ownership with struct update
Using ..old_user moves non-Copy fields like name (String) that are not explicitly overridden, while age is overridden with a new value.
Step 2: Check usage of old_user
old_user is not used after the update, so no ownership error occurs. The code compiles and runs, printing "Bob is 25 years old".
Final Answer:
It compiles and runs without errors -> Option C
Quick Check:
Non-Copy fields moved, but old_user not reused [OK]
Hint: Non-Copy fields move with ..old_struct, but OK if old not reused [OK]
Common Mistakes:
Thinking it causes a partial move error
Expecting automatic cloning of String
Assuming syntax error instead of checking ownership
You want to create a new Config with debug set to true, keep version the same, and change name to "NewApp". Which code correctly uses struct update syntax and avoids ownership errors?
hard
A. let new_config = Config { debug: true, name: String::from("NewApp"), ..old_config };
B. let new_config = Config { debug: true, ..old_config, name: String::from("NewApp") };
C. let new_config = Config { debug: true, name: "NewApp", ..old_config };
D. let new_config = Config { debug: true, name: old_config.name, ..old_config };
Solution
Step 1: Place changed fields before struct update
Changed fields debug and name must come before ..old_config.
Step 2: Avoid ownership errors by creating new String
Since name is a String, assign a new String with String::from("NewApp") to avoid moving old_config.name.
Final Answer:
let new_config = Config { debug: true, name: String::from("NewApp"), ..old_config }; -> Option A
Quick Check:
Changed fields first, clone String to avoid move [OK]
Hint: Clone or create new String before ..old_struct [OK]
Common Mistakes:
Placing ..old_config before changed fields
Using string literal instead of String::from
Moving old_config.name causing ownership error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.