Challenge - 5 Problems
Rust Struct Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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);
}Attempts:
2 left
💡 Hint
Remember that struct update syntax copies remaining fields from the given instance.
✗ Incorrect
The struct update syntax '..p1' copies all fields from p1 that are not explicitly set. Here, x is set to 15, y is copied from p1 which is 10, so output is (15, 10).
❓ Predict Output
intermediateStruct 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);
}Attempts:
2 left
💡 Hint
Consider what happens to ownership of String fields when using struct update syntax.
✗ Incorrect
The struct update syntax moves the ownership of p1.name into p2.name. Since p1.name is moved, p1 cannot be used after. But here p1 is partially moved, which causes a compilation error.
🔧 Debug
advancedWhy 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);
}Attempts:
2 left
💡 Hint
Think about ownership and usage of base after struct update.
✗ Incorrect
Struct update syntax moves non-Copy fields (String) from base. Here, since verbose is explicitly set, debug is moved from base to new_config. Using base.debug afterward causes compilation error due to use of partially moved value.
❓ Predict Output
advancedOutput 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);
}Attempts:
2 left
💡 Hint
Consider ownership of String fields when using struct update syntax.
✗ Incorrect
The struct update syntax moves base.title into new_window.title, so base.title is moved. This causes a compilation error if base is used after or if base is not fully moved. Here, base is partially moved, causing error.
🚀 Application
expertHow 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
};
}Attempts:
2 left
💡 Hint
Count all fields except the one explicitly set in the update syntax.
✗ Incorrect
The struct update syntax copies all fields except those explicitly set. Here, a is set to 20, so b, c, and d are copied from base, totaling 3 fields.
