泛型(Generics)
泛型参数
函数some_function
有一个参数t
的类型为泛型T
, 返回值的类型也是泛型T
fn some_function<T>(t: T) -> T {
// do some thing
}
多泛型参数
fn some_function<T, U>(t: T, u: U) -> i32 {
// do some thing
}
泛型限定
泛型T
的类型实现了Summarizable
pub fn notify<T: Summarizable>(item: T) {
// do some thing
}
多泛型限定
泛型T
是任何实现了Summarizable
和CloneObj
的类型
pub fn notify<T: Summarizable + CloneObj>(item: T) {
// do some thing
}
where从句写法
fn some_function<T: Display + Clone, U: Clone + Debug>(t: T, u: U) -> i32 {
// do some thing
}
和下面的写法意思一样
fn some_function<T, U>(t: T, u: U) -> i32
where T: Display + Clone, U: Clone + Debug{
// do some thing
}