Выбрать главу

}

// `T` must implement `HasArea`. Any type which meets

// the bound can access `HasArea`'s function `area`.

fn area<T: HasArea>(t: &T) -> f64 { t.area() }

fn main() {

let rectangle = Rectangle { length: 3.0, height: 4.0 };

let _triangle = Triangle { length: 3.0, height: 4.0 };

print_debug(&rectangle);

println!("Area: {}", area(&rectangle));

//print_debug(&_triangle);

//println!("Area: {}", area(&_triangle));

// ^ TODO: Try uncommenting these.

// | Error: Does not implement either `Debug` or `HasArea`.

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As an additional note, where clauses can also be used to apply bounds in some cases to be more expressive.

std::fmt, structs, and traits

A consequence of how bounds work is that even if a trait doesn't include any functionality, you can still use it as a bound. Eq and Copy are examples of such traits from the std library.

struct Cardinal;

struct BlueJay;

struct Turkey;

trait Red {}

trait Blue {}

impl Red for Cardinal {}

impl Blue for BlueJay {}

// These functions are only valid for types which implement these

// traits. The fact that the traits are empty is irrelevant.

fn red<T: Red>(_: &T) -> &'static str { "red" }

fn blue<T: Blue>(_: &T) -> &'static str { "blue" }

fn main() {

let cardinal = Cardinal;

let blue_jay = BlueJay;

let _turkey = Turkey;

// `red()` won't work on a blue jay nor vice versa

// because of the bounds.

println!("A cardinal is {}", red(&cardinal));

println!("A blue jay is {}", blue(&blue_jay));

//println!("A turkey is {}", red(&_turkey));

// ^ TODO: Try uncommenting this line.

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

std::cmp::Eq, std::marker::Copy, and traits

Multiple bounds can be applied with a +. Like normal, different types are separated with ,.

use std::fmt::{Debug, Display};

fn compare_prints<T: Debug + Display>(t: &T) {

println!("Debug: `{:?}`", t);

println!("Display: `{}`", t);

}

fn compare_types<T: Debug, U: Debug>(t: &T, u: &U) {

println!("t: `{:?}`", t);

println!("u: `{:?}`", u);

}

fn main() {

let string = "words";

let array = [1, 2, 3];

let vec = vec![1, 2, 3];

compare_prints(&string);

//compare_prints(&array);

// TODO ^ Try uncommenting this.

compare_types(&array, &vec);

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

std::fmt and traits

A bound can also be expressed using a where clause immediately before the opening {, rather than at the type's first mention. Additionally, where clauses can apply bounds to arbitrary types, rather than just to type parameters.

Some cases that a where clause is usefuclass="underline"

   • When specifying generic types and bounds separately is clearer:

impl <A: TraitB + TraitC, D: TraitE + TraitF> MyTrait<A, D> for YourType {}

// Expressing bounds with a `where` clause