Let us look at Rust Generics. You can start writing Generics in Rust as shown below:
struct Val {
val: f64,
}
struct GenVal<T> {
gen_val: T,
}
// impl of Val
impl Val {
fn value(&self) -> &f64 {
&self.val
}
}
// impl of GenVal for a generic type `T`
impl<T> GenVal<T> {
fn value(&self) -> &T {
&self.gen_val
}
}
fn main() {
let x = Val { val: 3.0 };
let y = GenVal { gen_val: 3i32 };
println!("{}, {}", x.value(), y.value());
}
The code above is compiled using the below command.
rustc generic_example.rs
The output will be as below:
apples-MacBook-Air:generics bhagvan.kommadi$ ls generic_example generic_example.rs apples-MacBook-Air:generics bhagvan.kommadi$ ./generic_example 5, 6 apples-MacBook-Air:generics bhagvan.kommadi$



