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

// println!("Old enough {}", old_enough(&age_days));

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Uncomment the last print statement to observe that the type supplied must be Years.

To obtain the newtype's value as the base type, you may use tuple syntax like so:

struct Years(i64);

fn main() {

let years = Years(42);

let years_as_primitive: i64 = years.0;

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

structs

<p id="associated_items"><strong><a l:href="#associated_items">Associated items</a></strong></p>

"Associated Items" refers to a set of rules pertaining to items of various types. It is an extension to trait generics, and allows traits to internally define new items.

One such item is called an associated type, providing simpler usage patterns when the trait is generic over its container type.

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

RFC

<p id="the_problem"><strong><a l:href="#the_problem">The Problem</a></strong></p>

A trait that is generic over its container type has type specification requirements - users of the trait must specify all of its generic types.

In the example below, the Contains trait allows the use of the generic types A and B. The trait is then implemented for the Container type, specifying i32 for A and B so that it can be used with fn difference().

Because Contains is generic, we are forced to explicitly state all of the generic types for fn difference(). In practice, we want a way to express that A and B are determined by the input C. As you will see in the next section, associated types provide exactly that capability.

struct Container(i32, i32);

// A trait which checks if 2 items are stored inside of container.

// Also retrieves first or last value.

trait Contains {

fn contains(&self, _: &A, _: &B) -> bool; // Explicitly requires `A` and `B`.

fn first(&self) -> i32; // Doesn't explicitly require `A` or `B`.

fn last(&self) -> i32; // Doesn't explicitly require `A` or `B`.

}

impl Contains for Container {

// True if the numbers stored are equal.

fn contains(&self, number_1: &i32, number_2: &i32) -> bool {

(&self.0 == number_1) && (&self.1 == number_2)

}

// Grab the first number.

fn first(&self) -> i32 { self.0 }

// Grab the last number.

fn last(&self) -> i32 { self.1 }

}

// `C` contains `A` and `B`. In light of that, having to express `A` and

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

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