Unit-like structs in Rust - Time & Space Complexity
Let's explore how the time it takes to run code with unit-like structs changes as we use more of them.
We want to know how the program's work grows when we create or use many unit-like structs.
Analyze the time complexity of the following code snippet.
struct Marker;
fn create_markers(n: usize) -> Vec<Marker> {
let mut v = Vec::new();
for _ in 0..n {
v.push(Marker);
}
v
}
This code creates a list of n unit-like structs called Marker.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that runs
ntimes to create and addMarkerstructs. - How many times: Exactly
ntimes, once for each struct created.
As n grows, the number of times we create and add structs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 creations and insertions |
| 100 | 100 creations and insertions |
| 1000 | 1000 creations and insertions |
Pattern observation: The work grows directly in step with n. Double n, double the work.
Time Complexity: O(n)
This means the time to create n unit-like structs grows in a straight line with n.
[X] Wrong: "Creating unit-like structs is instant and does not add to time as n grows."
[OK] Correct: Even though unit-like structs hold no data, creating and storing many of them still takes time proportional to how many you make.
Understanding how simple data structures like unit-like structs affect performance helps you explain your code choices clearly and confidently.
"What if we changed the loop to create two unit-like structs per iteration? How would the time complexity change?"
Practice
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 DQuick Check:
Unit-like struct = marker struct [OK]
- Thinking unit-like structs must have fields
- Confusing unit-like structs with tuple structs
- Assuming unit-like structs hold unit type values
Marker in Rust?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 AQuick Check:
Unit-like struct syntax = struct Name; [OK]
- Using parentheses like tuple structs
- Using braces like normal structs
- Adding square brackets which are invalid
struct Flag;
impl Flag {
fn is_set(&self) -> bool {
true
}
}
fn main() {
let f = Flag;
println!("{}", f.is_set());
}Solution
Step 1: Understand the struct and method
Flag is a unit-like struct with a methodis_setthat always returns true.Step 2: Trace the main function
We create an instancefof Flag and callf.is_set(), which returns true, printed as "true".Final Answer:
true -> Option BQuick Check:
Method returns true, output is true [OK]
- Expecting a compilation error due to no fields
- Thinking unit-like structs cannot have methods
- Confusing output with false or panic
struct Label;
fn main() {
let l = Label();
println!("Label created");
}Solution
Step 1: Check struct instantiation
Unit-like structs are instantiated without parentheses, just by name.Step 2: Identify the error
The code usesLabel()which is invalid; it should beLabel.Final Answer:
Unit-like structs cannot be instantiated with parentheses. -> Option CQuick Check:
Instantiate unit-like struct without () [OK]
- Adding parentheses when creating unit-like struct instances
- Forgetting semicolon after struct declaration (not in this code)
- Misusing println! macro syntax
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());
}
}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 AQuick Check:
Two markers print label twice [OK]
- Assuming unit-like structs cannot be stored in collections
- Expecting compilation error due to zero-sized type
- Thinking only one print occurs
Ready to prove it?
Pass the test and this pattern moves unknown
