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

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

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

std::fmt, structs, and traits

<p id="testcase_empty_bounds"><strong><a l:href="#testcase_empty_bounds">Testcase: empty bounds</a></strong></p>

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) -> &'static str { "red" }

fn 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

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

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

<p id="multiple_bounds"><strong><a l:href="#multiple_bounds">Multiple bounds</a></strong></p>

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

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

fn compare_prints(t: &T) {

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

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

}

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

}

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

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