what is Generics?
the essence of Generics is assuming it to be certain type。
the true type is not know until excution
- how to use generics in function?
fn <T,U>name(st:T):U{
}
- how to use generics in stuct?
stuct A <T,U> {
string:T
age:U
}
impl <T,U> A <T,U> {
fn say<T2>(&self,param:T2) -> T2 {
return param;
}
}
`3. how to use generics in enum?``
enum Result <T,U>{
Ok(T),
Err(U)
}
Top comments (0)