Generics
Last updated
Last updated
Generics in Rust enable you to write flexible, reusable code that works with different data types while ensuring type safety.
We use generics to create definitions for items like function signatures or structs, which we can then use with many different concrete data types.
Represented by angle brackets (<>
) and often denoted by T
, U
, or other descriptive names.
Rust generates optimized code for each concrete type used with a generic at compile time, avoiding runtime overhead.
References:
In this exercise we just need to define the vector type.
We can use i32
that satisfy both u8
and i8
type that pushed in the main
function.
In this exercise we tasked to build Wrapper
that accepts generics.
So we need to add <T>
in struct definition and the implementation to make it generics.