Rust by Example

14.1 Implementation

Implementations can also be made generic. Generally, impl is followed by the type <Type>, although it is not a strict requirement.

struct S; // A null struct
struct GenericTup<T>(T,);

// impl of GenericTup we specifically specialize:
impl GenericTup<f32> {} // Specialize to `f32`
impl GenericTup<S> {} // Specialize to `S` defined above

// `<T>` Must precede the type to remain generic
impl <T> GenericTup<T> {}
struct Tup (f64,); struct GenTup<T>(T,); // impl of Tup impl Tup { fn value(&self) -> &f64 { let &Tup ( ref val ) = self; val } } // impl of GenTup for a generic type `T` impl <T> GenTup<T> { fn value(&self) -> &T { let &GenTup (ref val) = self; val } } fn main() { let x = Tup(3.0); let y = GenTup(3i32); println!("{}, {}", x.value(), y.value()); }

See also:

impl, struct, and functions returning references,