Challenge - 5 Problems
Unit-like Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of printing a unit-like struct instance
What is the output of this Rust code?
struct Marker;
fn main() {
let m = Marker;
println!("{}", std::any::type_name_of_val(&m));
}
Rust
struct Marker;
fn main() {
let m = Marker;
println!("{}", std::any::type_name_of_val(&m));
}Attempts:
2 left
💡 Hint
Think about what type_name_of_val returns for a struct instance.
✗ Incorrect
The function std::any::type_name_of_val returns the full type name as a string. For the unit-like struct Marker, it returns "Marker".
❓ Predict Output
intermediateSize of a unit-like struct
What is the output of this Rust program?
struct Empty;
fn main() {
println!("{}", std::mem::size_of::());
}
Rust
struct Empty;
fn main() {
println!("{}", std::mem::size_of::<Empty>());
}Attempts:
2 left
💡 Hint
Consider how Rust represents unit-like structs in memory.
✗ Incorrect
Unit-like structs in Rust are zero-sized types (ZSTs) and have a size of 0 bytes.
🔧 Debug
advancedWhy does this code fail to compile?
Consider this Rust code:
Why does it fail to compile?
struct Flag;
fn main() {
let f = Flag();
}
Why does it fail to compile?
Rust
struct Flag;
fn main() {
let f = Flag();
}Attempts:
2 left
💡 Hint
Think about how unit-like structs are created compared to tuple structs.
✗ Incorrect
Unit-like structs are instantiated by just writing their name without parentheses. Using parentheses causes a syntax error.
🧠 Conceptual
advancedPurpose of unit-like structs in Rust
Which of the following best describes a common use case for unit-like structs in Rust?
Attempts:
2 left
💡 Hint
Think about how Rust uses types to represent concepts beyond data storage.
✗ Incorrect
Unit-like structs are often used as markers or flags by implementing traits without storing data.
❓ Predict Output
expertOutput of pattern matching with unit-like structs
What is the output of this Rust program?
struct Signal;
fn check_signal(s: Signal) -> &'static str {
match s {
Signal => "Received Signal",
}
}
fn main() {
let s = Signal;
println!("{}", check_signal(s));
}
Rust
struct Signal;
fn check_signal(s: Signal) -> &'static str {
match s {
Signal => "Received Signal",
}
}
fn main() {
let s = Signal;
println!("{}", check_signal(s));
}Attempts:
2 left
💡 Hint
Unit-like structs can be matched by their name without parentheses.
✗ Incorrect
The pattern Signal matches the unit-like struct instance s correctly, so the function returns "Received Signal".
