Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

테스트케이스: 빈 바운드

바운드가 작동하는 방식의 결과로, trait가 아무런 기능을 포함하지 않더라도 여전히 바운드로 사용할 수 있습니다. std 라이브러리의 EqCopy가 그러한 trait의 예입니다.

struct Cardinal;
struct BlueJay;
struct Turkey;

trait Red {}
trait Blue {}

impl Red for Cardinal {}
impl Blue for BlueJay {}

// 이 함수들은 이 트레이트들을 구현하는 타입에 대해서만 유효합니다.
// 트레이트가 비어 있다는 사실은 무관합니다.
fn red<T: Red>(_: &T)   -> &'static str { "빨강" }
fn blue<T: Blue>(_: &T) -> &'static str { "파랑" }

fn main() {
    let cardinal = Cardinal;
    let blue_jay = BlueJay;
    let _turkey   = Turkey;

    // 바운드 때문에 `red()`는 blue jay(파랑 어치)에 대해 작동하지 않으며
// 그 반대도 마찬가지입니다.
    println!("홍관조(cardinal)는 {}입니다", red(&cardinal));
    println!("파랑 어치(blue jay)는 {}입니다", blue(&blue_jay));
    //println!("칠면조는 {}", red(&_turkey));
// ^ TODO: 이 줄의 주석을 해제해 보세요.
}

참고:

std::cmp::Eq, std::marker::Copy, 그리고 trait