Читаем Rust by Example полностью

// Because `SGen` is preceded by ``, this function is generic over `T`.

fn generic(_s: SGen) {}

fn main() {

// Using the non-generic functions

reg_fn(S(A)); // Concrete type.

gen_spec_t(SGen(A)); // Implicitly specified type parameter `A`.

gen_spec_i32(SGen(6)); // Implicitly specified type parameter `i32`.

// Explicitly specified type parameter `char` to `generic()`.

generic::(SGen('a'));

// Implicitly specified type parameter `char` to `generic()`.

generic(SGen('c'));

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="see_also_33"><strong><a l:href="#see_also_33">See also:</a></strong></p>

functions and structs

<p id="implementation"><strong><a l:href="#implementation">Implementation</a></strong></p>

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

<p id="see_also_34"><strong><a l:href="#see_also_34">See also:</a></strong></p>

functions returning references, impl, and struct

<p id="traits"><strong><a l:href="#traits">Traits</a></strong></p>

Of course traits can also be generic. Here we define one which reimplements the Drop trait as a generic method to drop itself and an input.

// Non-copyable types.

struct Empty;

struct Null;

// A trait generic over `T`.

trait DoubleDrop {

// Define a method on the caller type which takes an

Перейти на страницу:

Похожие книги