DEV Community

Zhangwuji
Zhangwuji

Posted on

rust Generics

what is Generics?
the essence of Generics is assuming it to be certain type。
the true type is not know until excution

  1. how to use generics in function?
fn <T,U>name(st:T):U{

}
Enter fullscreen mode Exit fullscreen mode
  1. 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)

}






Enter fullscreen mode Exit fullscreen mode

Top comments (0)