A generic struct in Rust is a struct that can hold data of any type, specified when the struct is used. It uses type parameters to be flexible and reusable.
Click to reveal answer
beginner
How do you define a generic struct with one type parameter in Rust?
You add angle brackets with a type parameter after the struct name, like <T>. For example:
struct Point<T> {
x: T,
y: T,
}
Click to reveal answer
intermediate
Can a generic struct have multiple type parameters? How?
Yes. You list multiple type parameters separated by commas inside the angle brackets. For example:
struct Pair {
first: T,
second: U,
}
Click to reveal answer
beginner
How do you create an instance of a generic struct with a specific type?
You specify the type when creating the instance or let Rust infer it. For example: <br><pre>let p = Point { x: 5, y: 10 }; // Rust infers T = i32
let p2: Point<f64> = Point { x: 1.0, y: 4.0 };</pre>
Click to reveal answer
beginner
Why use generic structs instead of fixed-type structs?
Generic structs let you write flexible and reusable code. You can use the same struct for different data types without rewriting it for each type.
Click to reveal answer
How do you declare a generic struct named Container with one type parameter T?
Astruct Container<T> value: T;
Bstruct Container<T> { value: T }
Cstruct Container { T value }
Dstruct Container<T> (value: T);
✗ Incorrect
The correct syntax uses angle brackets after the struct name to declare the type parameter T.
What does this code do? struct Pair { first: T, second: U }
ADefines a struct with fixed types T and U
BDefines a struct with one generic type T
CDefines a struct with two generic types T and U
DDefines a function with generic parameters
✗ Incorrect
The struct Pair has two generic type parameters T and U, allowing it to hold two values of possibly different types.
How do you create an instance of Point<f64> with x=1.0 and y=2.0?
Alet p = Point<f64>(1.0, 2.0);
Blet p: Point<f64> = Point { x: 1.0, y: 2.0 };
Clet p = Point { x: 1.0, y: 2.0 };
DBoth B and C are correct
✗ Incorrect
Rust can infer the type from the values or you can specify it explicitly.
Why are generic structs useful?
AThey allow code reuse for different data types
BThey make code slower
CThey restrict structs to one type only
DThey are only used for functions
✗ Incorrect
Generic structs let you write one struct that works with many types, improving code reuse.
Which of these is NOT a valid generic struct declaration?
Astruct Box { T value }
Bstruct Info<T, U> { first: T, second: U }
Cstruct Wrapper<T> (T);
Dstruct Data<T> { value: T }
✗ Incorrect
Option A is invalid because it does not declare T as a generic parameter properly.
Explain what a generic struct is and how you define one in Rust.
Think about how you make a struct work with any type.
You got /3 concepts.
Describe how to create and use an instance of a generic struct with multiple type parameters.
Remember to specify types or let Rust infer them.
You got /3 concepts.
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.