0
0
Rustprogramming~10 mins

Generic structs in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic structs
Define struct with <T>
Create instance with specific type
Use fields of instance
Generic type allows flexibility
This flow shows how a struct is defined with a generic type, then created with a specific type, and finally used.
Execution Sample
Rust
struct Point<T> {
    x: T,
    y: T,
}

fn main() {
    let p = Point { x: 5, y: 10 };
}
Defines a generic struct Point and creates an instance with integers.
Execution Table
StepActionCode LineState ChangeNotes
1Define generic struct Point<T>struct Point<T> { x: T, y: T }Point<T> type createdGeneric placeholder T introduced
2Create instance p with T = i32let p = Point { x: 5, y: 10 };p.x = 5, p.y = 10T replaced by i32 for this instance
3Access p.xp.xValue = 5Using field of generic struct instance
4Access p.yp.yValue = 10Using field of generic struct instance
5End--Program ends after instance creation and access
💡 All steps complete; generic struct used with concrete type i32
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p.x-5555
p.y-10101010
Key Moments - 3 Insights
Why do we write <T> after struct name?
The <T> tells Rust this struct uses a generic type placeholder. In the execution_table step 1, this placeholder is defined but not yet a real type.
When does T become a real type like i32?
In step 2 of the execution_table, when we create the instance p with x=5 and y=10, Rust infers T as i32 for this instance.
Can p.x and p.y have different types?
No, because the struct uses the same generic T for both fields. So p.x and p.y must be the same type, shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what type does T become for instance p?
Ai32
Bf64
CString
Dbool
💡 Hint
Check the values assigned to p.x and p.y in step 2; they are integers.
At which step do we first use the fields of the generic struct?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the Action column in execution_table; step 3 accesses p.x.
If we changed p.y to a string, what would happen in the execution_table?
Ap.x would convert to String automatically
BT would be inferred as String
CCompilation error because x and y types differ
DNo change, code runs fine
💡 Hint
Recall key_moments answer about both fields sharing the same generic type T.
Concept Snapshot
Generic structs allow defining structs with placeholder types <T>.
When creating an instance, T is replaced by a real type (e.g., i32).
All fields using T share the same type.
This enables flexible and reusable data structures.
Full Transcript
This example shows how to define a generic struct Point with a type placeholder T. When we create an instance p with x=5 and y=10, Rust infers T as i32. We then access p.x and p.y fields which hold integer values. The generic struct allows us to reuse the same structure with different types by replacing T with the desired type during instance creation.