- 实现
- 参见:
- 参见:
实现
和函数类似,实现(implementation)也需要关注保持泛型。(原文:Similar to functions, implementations require care to remain generic.)
struct S; // 具体类型 `S`struct GenericVal<T>(T,); // 泛型类型 `GenericVal`// GenericVal 的实现,此处我们显式地指定了类型参量:impl GenericVal<f32> {} // 指定 `f32` 类型impl GenericVal<S> {} // 指定为上面定义的 `S`// `<T>` 必须在类型之前给出来以保持泛型。// (原文:`<T>` Must precede the type to remain generic)impl <T> GenericVal<T> {}
struct Val {val: f64}struct GenVal<T>{gen_val: T}// Val 的实现(impl)impl Val {fn value(&self) -> &f64 { &self.val }}// GenVal 针对泛型类型 `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());}
参见:
函数返回引用, impl, 和 struct
