Generic structs let you create flexible data containers that can hold any type of data. This helps you reuse code without repeating it for each data type.
When you want a struct to work with different types of data without rewriting it.
When you have a collection or container that can hold various data types.
When you want to write functions or methods that work with many types inside a struct.
When you want to keep your code clean and avoid duplication for similar structs with different types.
Syntax
Rust
struct StructName<T> {
field: T,
}
The <T> after the struct name means it uses a generic type called T.
You can use any name instead of T, but T is common and stands for 'Type'.
Examples
A struct Point that can hold any type for x and y.
Rust
struct Point<T> {
x: T,
y: T,
}
A struct with two different generic types T and U.
Rust
struct Pair<T, U> {
first: T,
second: U,
}
Creating a Point with integers.
Rust
let int_point = Point { x: 5, y: 10 };
Creating a Point with floating-point numbers.
Rust
let float_point = Point { x: 1.2, y: 3.4 };
Sample Program
This program creates two Point structs with different types: integers and floats. It then prints their values.
Rust
struct Point<T> {
x: T,
y: T,
}
fn main() {
let int_point = Point { x: 5, y: 10 };
let float_point = Point { x: 1.2, y: 3.4 };
println!("int_point: x = {}, y = {}", int_point.x, int_point.y);
println!("float_point: x = {}, y = {}", float_point.x, float_point.y);
}
OutputSuccess
Important Notes
Generic structs help avoid writing many similar structs for different types.
You can add methods to generic structs using impl<T> StructName<T>.
Rust checks types at compile time, so using generics is safe and efficient.
Summary
Generic structs let you create flexible containers for any data type.
Use <T> after the struct name to define a generic type.
They help keep your code clean and reusable.
Practice
(1/5)
1. What does the <T> mean in the definition of a generic struct in Rust?
easy
A. It specifies a fixed type that the struct must always use.
B. It declares a placeholder type that can be replaced with any type when used.
C. It is used to define a function inside the struct.
D. It marks the struct as unsafe to use.
Solution
Step 1: Understand generic type parameters
The <T> syntax declares a generic type parameter named T, which acts as a placeholder for any type.
Step 2: Recognize its purpose in structs
This allows the struct to be flexible and work with any data type specified when creating an instance.
Final Answer:
It declares a placeholder type that can be replaced with any type when used. -> Option B
Quick Check:
Generic type parameter = placeholder type [OK]
Hint: Generic <T> means flexible type placeholder [OK]
Common Mistakes:
Thinking <T> fixes the type permanently
Confusing <T> with function syntax
Assuming <T> makes the struct unsafe
2. Which of the following is the correct syntax to define a generic struct named Container with one generic type parameter T?
easy
A. struct Container<T> { value: T }
B. struct Container { T value }
C. struct Container<T> (value: T);
D. struct Container<T> = { value: T };
Solution
Step 1: Recall Rust struct syntax with generics
Generic structs use struct Name<T> { field: T } syntax.
Step 2: Check each option
struct Container<T> { value: T } matches the correct syntax with curly braces and generic parameter T. Options A, B, and D have syntax errors or wrong struct declaration styles.