Bird
Raised Fist0
Rustprogramming~20 mins

Struct update syntax in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Struct Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of struct update syntax with partial fields
What is the output of this Rust program?
Rust
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p1 = Point { x: 5, y: 10 };
    let p2 = Point { x: 15, ..p1 };
    println!("({}, {})", p2.x, p2.y);
}
A(15, 10)
B(5, 10)
C(15, 0)
DCompilation error
Attempts:
2 left
💡 Hint
Remember that struct update syntax copies remaining fields from the given instance.
❓ Predict Output
intermediate
2:00remaining
Struct update syntax with ownership and String fields
What will this Rust program print?
Rust
struct Person {
    name: String,
    age: u8,
}

fn main() {
    let p1 = Person { name: String::from("Alice"), age: 30 };
    let p2 = Person { age: 25, ..p1 };
    println!("{} is {} years old", p2.name, p2.age);
    println!("Original name: {}", p1.name);
}
AAlice is 30 years old
BAlice is 25 years old
CRuntime panic
DCompilation error due to move of p1.name
Attempts:
2 left
💡 Hint
Consider what happens to ownership of String fields when using struct update syntax.
🔧 Debug
advanced
2:00remaining
Why does this struct update code fail to compile?
This Rust code fails to compile. What is the reason?
Rust
struct Config {
    debug: String,
    verbose: bool,
}

fn main() {
    let base = Config { debug: String::from("enabled"), verbose: false };
    let new_config = Config { verbose: true, ..base };
    println!("Debug: {}, Verbose: {}", base.debug, new_config.verbose);
}
AMissing semicolon after struct initialization
BField verbose is not found in struct Config
Cbase is partially moved and cannot be used after struct update
DCannot use struct update syntax with boolean fields
Attempts:
2 left
💡 Hint
Think about ownership and usage of base after struct update.
❓ Predict Output
advanced
2:00remaining
Output of struct update with nested structs
What is the output of this Rust program?
Rust
struct Dimensions {
    width: u32,
    height: u32,
}

struct Window {
    title: String,
    dims: Dimensions,
}

fn main() {
    let base = Window {
        title: String::from("Main"),
        dims: Dimensions { width: 800, height: 600 },
    };
    let new_window = Window {
        dims: Dimensions { width: 400, height: 300 },
        ..base
    };
    println!("{}: {}x{}", new_window.title, new_window.dims.width, new_window.dims.height);
    println!("Original title: {}", base.title);
}
ACompilation error due to move of base.title
BMain: 800x600
CPopup: 0x0
DPopup: 800x600
Attempts:
2 left
💡 Hint
Consider ownership of String fields when using struct update syntax.
🚀 Application
expert
3:00remaining
How many fields are copied in this struct update?
Given this Rust code, how many fields are copied from base to new_struct using struct update syntax?
Rust
struct Data {
    a: i32,
    b: i32,
    c: f64,
    d: bool,
}

fn main() {
    let base = Data {
        a: 10,
        b: 42,
        c: 3.14,
        d: true,
    };
    let new_struct = Data {
        a: 20,
        ..base
    };
}
A4 fields are copied
B3 fields are copied
C1 field is copied
D2 fields are copied
Attempts:
2 left
💡 Hint
Count all fields except the one explicitly set in the update syntax.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Copies all remaining fields from old_struct to the new struct -> Option A
  4. Quick Check:

    Struct update syntax copies fields [OK]
Hint: Remember: ..old_struct copies leftover fields [OK]
Common Mistakes:
  • Thinking it moves fields instead of copying
  • Assuming it deletes the old struct
  • Confusing it with references
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

  1. Step 1: Check correct syntax order

    In Rust, fields you want to change come first, then ..old_struct copies the rest.
  2. 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.
  3. Final Answer:

    let new_user = User { name: "Alice", ..old_user }; -> Option B
  4. 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

  1. 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.
  2. Step 2: Determine printed values

    Printing p2.x and p2.y outputs (8, 10).
  3. Final Answer:

    (8, 10) -> Option D
  4. 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

  1. 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.
  2. 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".
  3. Final Answer:

    It compiles and runs without errors -> Option C
  4. 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
5. You have a struct:
struct Config { debug: bool, version: u32, name: String }

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

  1. Step 1: Place changed fields before struct update

    Changed fields debug and name must come before ..old_config.
  2. 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.
  3. Final Answer:

    let new_config = Config { debug: true, name: String::from("NewApp"), ..old_config }; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard