📖 Scenario: Imagine you are building a simple Rust program to represent different states or markers without any data. Unit-like structs are perfect for this because they act like named flags or markers.
🎯 Goal: You will create a unit-like struct, use it to mark a state, and print a message confirming the state.
📋 What You'll Learn
Create a unit-like struct named Active
Create a variable called user_status of type Active
Implement a function called print_status that takes status: Active and prints "User is active"
Call print_status with user_status and print the output
💡 Why This Matters
🌍 Real World
Unit-like structs are useful to represent simple states or markers without extra data, like flags or tags in programs.
💼 Career
Understanding unit-like structs helps in Rust programming for clear and efficient code, especially in systems programming and state management.
Progress0 / 4 steps
1
Create a unit-like struct
Create a unit-like struct called Active with no fields.
Rust
Hint
Unit-like structs have no fields and end with a semicolon.
2
Create a variable of the unit-like struct type
Create a variable called user_status and assign it the value Active.
Rust
Hint
Instantiate the unit-like struct by writing its name without parentheses.
3
Write a function that takes the unit-like struct
Write a function called print_status that takes a parameter status of type Active and prints "User is active".
Rust
Hint
The function takes status: Active and uses println! to print the message.
4
Call the function and print the output
Call the function print_status with the variable user_status.
Rust
Hint
Call print_status(user_status); to see the message.
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
Step 1: Understand struct definitions
Structs in Rust can have fields or no fields. A unit-like struct has no fields.
Step 2: Identify the purpose of unit-like structs
They are used as markers or labels without storing data.
Final Answer:
A struct without any fields, used as a marker. -> Option D
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
Step 1: Recall unit-like struct syntax
Unit-like structs are declared with just the name and a semicolon, no parentheses or braces.
Step 2: Compare options
struct Marker; matches the syntax: struct Marker;
Final Answer:
struct Marker; -> Option A
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
Step 1: Understand the struct and method
Flag is a unit-like struct with a method is_set that always returns true.
Step 2: Trace the main function
We create an instance f of Flag and call f.is_set(), which returns true, printed as "true".
Final Answer:
true -> Option B
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
Step 1: Check struct instantiation
Unit-like structs are instantiated without parentheses, just by name.
Step 2: Identify the error
The code uses Label() which is invalid; it should be Label.
Final Answer:
Unit-like structs cannot be instantiated with parentheses. -> Option C
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
Step 1: Understand vector of unit-like structs
Unit-like structs can be stored in vectors as they are zero-sized but valid types.
Step 2: Trace the loop and method calls
The loop prints the label for each Marker instance, which returns "Unit Marker" twice.
Final Answer:
Unit Marker\nUnit Marker -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.