Challenge - 5 Problems
Tuple Struct Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of tuple struct field access
What is the output of this Rust code?
Rust
struct Color(u8, u8, u8);
fn main() {
let black = Color(0, 0, 0);
println!("{}", black.1);
}Attempts:
2 left
💡 Hint
Tuple struct fields are accessed by dot and index starting at 0.
✗ Incorrect
The tuple struct Color has three fields indexed 0, 1, and 2. black.1 accesses the second field which is 0.
❓ Predict Output
intermediateOutput of tuple struct destructuring
What does this Rust program print?
Rust
struct Point(i32, i32);
fn main() {
let p = Point(5, 10);
let Point(x, y) = p;
println!("{} {}", x, y);
}Attempts:
2 left
💡 Hint
Destructuring assigns fields in order to variables.
✗ Incorrect
The tuple struct Point(5, 10) is destructured into x=5 and y=10, so it prints '5 10'.
🔧 Debug
advancedWhy does this tuple struct code fail to compile?
This Rust code does not compile. What is the cause of the error?
Rust
struct Size(u32, u32);
fn main() {
let s = Size { 10, 20 };
println!("{} {}", s.0, s.1);
}Attempts:
2 left
💡 Hint
Tuple structs use parentheses for initialization, not curly braces.
✗ Incorrect
Tuple structs are initialized with parentheses and positional values, not with curly braces and named fields.
📝 Syntax
advancedWhich option correctly defines a tuple struct?
Select the correct Rust syntax to define a tuple struct named 'Rectangle' with two i32 fields.
Attempts:
2 left
💡 Hint
Tuple structs use parentheses and commas between types.
✗ Incorrect
Option A uses correct tuple struct syntax with parentheses and comma-separated types.
🚀 Application
expertHow many elements does this tuple struct instance contain?
Given the tuple struct and instance below, how many elements does 'data' contain?
Rust
struct Data(i32, f64, char, bool); fn main() { let data = Data(42, 3.14, 'x', true); // How many elements are in 'data'? }
Attempts:
2 left
💡 Hint
Count the number of types inside the tuple struct definition.
✗ Incorrect
The tuple struct Data has four fields, so the instance 'data' contains 4 elements.
