Bird
Raised Fist0
Rustprogramming~10 mins

Unit-like structs in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Unit-like structs
Define unit-like struct
↓
Create instance
↓
Use instance (no data inside)
↓
Pass or return instance
↓
End
Unit-like structs have no fields and act like markers or flags. You define, create, and use them without storing data.
Execution Sample
Rust
struct Marker;

fn main() {
    let m = Marker;
    println!("Instance created: {}", std::any::type_name::<Marker>());
}
Defines a unit-like struct Marker, creates an instance, and prints its type name.
Execution Table
StepActionCode LineResult/State
1Define unit-like struct Markerstruct Marker;Marker type exists, no data fields
2Enter main functionfn main() {Ready to create instance
3Create instance m of Markerlet m = Marker;m is instance of Marker, no data inside
4Print type name of mprintln!("Instance created: {}", std::any::type_name::<Marker>());Output: Instance created: Marker
5End main function}Program ends
💡 Program ends after printing instance type name
Variable Tracker
VariableStartAfter Step 3Final
mundefinedMarker instance (no data)Marker instance (no data)
Key Moments - 3 Insights
Why does Marker have no parentheses or fields when creating an instance?
Because Marker is a unit-like struct with no fields, you create it simply by writing its name without parentheses or data, as shown in execution_table step 3.
Can we store data inside a unit-like struct instance?
No, unit-like structs have no fields, so instances hold no data. This is why in variable_tracker, m has no data after creation.
What is the purpose of a unit-like struct if it holds no data?
It acts as a marker or flag type to indicate something in code, useful for type safety or traits, as seen in the concept_flow where it is created and passed without data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the value of variable m?
AAn instance of Marker with no data
BAn integer zero
CA string "Marker"
DUndefined
💡 Hint
Check execution_table row 3 under Result/State for variable m
At which step does the program print output?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table step 4 where println! is called
If Marker had fields, how would creating an instance change compared to step 3?
AYou would still write 'let m = Marker;'
BYou would write 'let m = Marker();'
CYou would write 'let m = Marker { field: value };'
DYou cannot create instances of structs with fields
💡 Hint
Recall that unit-like structs have no fields, so step 3 shows no braces; with fields, braces and values are needed
Concept Snapshot
Unit-like structs have no fields and are defined with a semicolon after the name.
Create instances by writing the struct name without parentheses or braces.
They hold no data and act as markers or flags.
Useful for type safety and traits.
Example: struct Marker; let m = Marker;
Full Transcript
This example shows how to define and use a unit-like struct in Rust. First, the struct Marker is defined with no fields, indicated by a semicolon. In the main function, an instance m of Marker is created simply by writing 'Marker' without parentheses or braces. The program then prints the type name of m, showing 'Marker'. Unit-like structs hold no data and serve as markers or flags in code. The variable m holds an instance of Marker but contains no data. This pattern is useful for signaling or implementing traits without storing values.

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