Module lessons (1/2)
Generics and Functions
Generics allow you to write flexible, reusable code, avoiding duplication of logic for different data types. Instead of defining multiple functions or structures for different types (like i32, f64, or String), we can use a generic type parameter, conventionally represented by the letter T.
The Rust compiler manages generics using a process called monomorphization: during compilation, it generates a copy of the generic code for each concrete type that is actually used. This means there is zero runtime performance overhead.
Generic Functions
To define a generic function, we insert the type parameter <T> right after the function name and before the argument list:
fn print_value<T: std::fmt::Debug>(value: T) {
println!("Value: {:?}", value);
}
In generic functions, we can use the generic type T for both argument types and return types:
fn identity<T>(value: T) -> T {
value
}
Generic Structs
We can also use generic type parameters inside structures (struct) to define flexible fields:
struct KeyValuePair<K, V> {
key: K,
value: V,
}
fn main() {
let pair = KeyValuePair {
key: String::from("age"),
value: 30,
};
}
In an impl block for a generic struct, we must declare the type parameter <T> immediately after the impl keyword to indicate we are implementing methods on a generic structure:
struct Container<T> {
value: T,
}
impl<T> Container<T> {
fn new(value: T) -> Self {
Container { value }
}
fn value(&self) -> &T {
&self.value
}
}
Try it yourself
Exercise 1: The Point Struct
Define a generic struct named Point<T> with two fields: x of type T and y of type T. In the main function, instantiate a variable point containing a Point with integer values x = 5 and y = 10, then print the value of point.x.
Show hint
Declare the struct using `struct Point<T> { x: T, y: T }`. Instantiate it in main and use `point.x` to print it.
Solution available after 3 attempts
Exercise 2: Reversing a Tuple with swap
Write a generic function named swap<T> that accepts a tuple of two elements (T, T) and returns a new tuple (T, T) with their positions reversed. In the main function, call the function with tuple (1, 2) and print the result.
Show hint
The function signature must be `fn swap<T>(pair: (T, T)) -> (T, T)`. Return the reversed tuple with `(pair.1, pair.0)`.
Solution available after 3 attempts
Exercise 3: The Container Struct
Define a generic struct Container<T> containing a value field of type T. Implement a generic impl block to define a new method that accepts a value of type T and returns an instance of Container<T>.
Show hint
Use `impl<T> Container<T>`to implement the associated method`fn new(value: T) -> Self { Container { value } }`.
Solution available after 3 attempts