Implementation

Similar to functions, implementations require care to remain generic.


#![allow(unused)]

fn main() {

struct S; // Concrete type `S`

struct GenericVal(T); // Generic type `GenericVal`


// impl of GenericVal where we explicitly specify type parameters:

impl GenericVal {} // Specify `f32`

impl GenericVal {} // Specify `S` as defined above


// `` Must precede the type to remain generic

impl GenericVal {}

}

struct Val {

val: f64,

}

struct GenVal {

gen_val: T,

}

// impl of Val

impl Val {

fn value(&self) -> &f64 {

&self.val

}

}

// impl of GenVal for a generic type `T`

impl GenVal {

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());

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

See also:

functions returning references, impl, and struct

Загрузка...