📖 Scenario: You are building a simple program to store pairs of values. Sometimes these values are numbers, sometimes text, or even a mix. Using generic structs in Rust helps you create one flexible container that works with any type.
🎯 Goal: Create a generic struct called Pair that can hold two values of any type. Then, create an instance of this struct with specific types and print its contents.
📋 What You'll Learn
Define a generic struct named Pair with two fields: first and second.
Create a variable called pair that holds a Pair of i32 and f64.
Assign the values 10 to first and 20.5 to second.
Print the values of pair.first and pair.second.
💡 Why This Matters
🌍 Real World
Generic structs let you write flexible code that works with many data types, useful in libraries and applications where data types vary.
💼 Career
Understanding generics is important for Rust developers to write reusable and type-safe code, a key skill in systems programming and backend development.
Progress0 / 4 steps
1
Define a generic struct
Define a generic struct called Pair with two fields: first and second. Use generic type parameters T and U for the field types.
Rust
Hint
Use struct Pair<T, U> { first: T, second: U } to define the generic struct.
2
Create an instance of the generic struct
Create a variable called pair that holds a Pair with i32 as T and f64 as U. Assign 10 to first and 20.5 to second.
Rust
Hint
Use let pair = Pair { first: 10, second: 20.5 }; to create the instance.
3
Implement a method to display the pair
Implement a method called display for the Pair struct that prints the values of first and second. Use impl<T: std::fmt::Display, U: std::fmt::Display> Pair<T, U> to allow printing.
Rust
Hint
Use an impl block with trait bounds T: Display and U: Display to define the display method.
4
Call the display method to show the pair
Call the display method on the pair variable to print its values.
Rust
Hint
Call pair.display(); to print the values.
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.