Bird
Raised Fist0
Rustprogramming~20 mins

Unit-like structs in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unit-like Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
}
A"Marker"
B"()"
C"struct Marker"
D"unit-like struct"
Attempts:
2 left
💡 Hint
Think about what type_name_of_val returns for a struct instance.
❓ Predict Output
intermediate
2:00remaining
Size 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>());
}
A4
B1
C0
D8
Attempts:
2 left
💡 Hint
Consider how Rust represents unit-like structs in memory.
🔧 Debug
advanced
2:00remaining
Why does this code fail to compile?
Consider this Rust code:
struct Flag;

fn main() {
let f = Flag();
}

Why does it fail to compile?
Rust
struct Flag;

fn main() {
    let f = Flag();
}
AUnit-like structs must have at least one field to be instantiated.
BUnit-like structs require a semicolon after the name when instantiated.
CUnit-like structs cannot be instantiated at all.
DUnit-like structs are instantiated without parentheses, so Flag() is invalid syntax.
Attempts:
2 left
💡 Hint
Think about how unit-like structs are created compared to tuple structs.
🧠 Conceptual
advanced
2:00remaining
Purpose of unit-like structs in Rust
Which of the following best describes a common use case for unit-like structs in Rust?
ATo create types that carry no data but can implement traits to mark behavior or state.
BTo allocate zero bytes of memory for optimization.
CTo represent empty tuples in function arguments.
DTo store multiple unnamed values in a compact form.
Attempts:
2 left
💡 Hint
Think about how Rust uses types to represent concepts beyond data storage.
❓ Predict Output
expert
2:00remaining
Output 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));
}
ACompilation error due to missing parentheses in pattern
BReceived Signal
CSignal
DRuntime panic due to non-exhaustive match
Attempts:
2 left
💡 Hint
Unit-like structs can be matched by their name without parentheses.

Practice

(1/5)
1. What is a unit-like struct in Rust?
easy
A. A struct that can only be used inside functions.
B. A struct with exactly one field.
C. A struct that holds only unit type values.
D. A struct without any fields, used as a marker.

Solution

  1. Step 1: Understand struct definitions

    Structs in Rust can have fields or no fields. A unit-like struct has no fields.
  2. Step 2: Identify the purpose of unit-like structs

    They are used as markers or labels without storing data.
  3. Final Answer:

    A struct without any fields, used as a marker. -> Option D
  4. Quick Check:

    Unit-like struct = marker struct [OK]
Hint: No fields means unit-like struct, used as marker [OK]
Common Mistakes:
  • Thinking unit-like structs must have fields
  • Confusing unit-like structs with tuple structs
  • Assuming unit-like structs hold unit type values
2. Which of the following is the correct syntax to declare a unit-like struct named Marker in Rust?
easy
A. struct Marker;
B. struct Marker();
C. struct Marker {}
D. struct Marker[];

Solution

  1. Step 1: Recall unit-like struct syntax

    Unit-like structs are declared with just the name and a semicolon, no parentheses or braces.
  2. Step 2: Compare options

    struct Marker; matches the syntax: struct Marker;
  3. Final Answer:

    struct Marker; -> Option A
  4. Quick Check:

    Unit-like struct syntax = struct Name; [OK]
Hint: Unit-like struct ends with semicolon, no braces [OK]
Common Mistakes:
  • Using parentheses like tuple structs
  • Using braces like normal structs
  • Adding square brackets which are invalid
3. What will be the output of this Rust code?
struct Flag;

impl Flag {
    fn is_set(&self) -> bool {
        true
    }
}

fn main() {
    let f = Flag;
    println!("{}", f.is_set());
}
medium
A. false
B. true
C. Compilation error
D. Runtime panic

Solution

  1. Step 1: Understand the struct and method

    Flag is a unit-like struct with a method is_set that always returns true.
  2. Step 2: Trace the main function

    We create an instance f of Flag and call f.is_set(), which returns true, printed as "true".
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Method returns true, output is true [OK]
Hint: Unit-like structs can have methods returning values [OK]
Common Mistakes:
  • Expecting a compilation error due to no fields
  • Thinking unit-like structs cannot have methods
  • Confusing output with false or panic
4. Identify the error in this Rust code using a unit-like struct:
struct Label;

fn main() {
    let l = Label();
    println!("Label created");
}
medium
A. Unit-like structs must have at least one field.
B. Missing semicolon after struct declaration.
C. Unit-like structs cannot be instantiated with parentheses.
D. The println! macro is used incorrectly.

Solution

  1. Step 1: Check struct instantiation

    Unit-like structs are instantiated without parentheses, just by name.
  2. Step 2: Identify the error

    The code uses Label() which is invalid; it should be Label.
  3. Final Answer:

    Unit-like structs cannot be instantiated with parentheses. -> Option C
  4. Quick Check:

    Instantiate unit-like struct without () [OK]
Hint: Instantiate unit-like structs by name only, no () [OK]
Common Mistakes:
  • Adding parentheses when creating unit-like struct instances
  • Forgetting semicolon after struct declaration (not in this code)
  • Misusing println! macro syntax
5. Given this code, what is the output?
struct Marker;

impl Marker {
    fn label(&self) -> &'static str {
        "Unit Marker"
    }
}

fn main() {
    let markers = vec![Marker, Marker];
    for m in markers {
        println!("{}", m.label());
    }
}
hard
A. Unit Marker\nUnit Marker
B. Compilation error due to vector of unit-like structs
C. Unit Marker printed once
D. Runtime panic due to missing fields

Solution

  1. Step 1: Understand vector of unit-like structs

    Unit-like structs can be stored in vectors as they are zero-sized but valid types.
  2. Step 2: Trace the loop and method calls

    The loop prints the label for each Marker instance, which returns "Unit Marker" twice.
  3. Final Answer:

    Unit Marker\nUnit Marker -> Option A
  4. Quick Check:

    Two markers print label twice [OK]
Hint: Unit-like structs can be stored and iterated like normal structs [OK]
Common Mistakes:
  • Assuming unit-like structs cannot be stored in collections
  • Expecting compilation error due to zero-sized type
  • Thinking only one print occurs
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