12.4.1 Testcase: empty bounds
바인드가 동작하는 방식의 결과로 trait이 어떤 기능성도 포함하고 있지 않더라도, 당신은 여전히 이를 바인드 대상으로 사용할 수 있다. Eq와 Ord는 std 라이브러리에 있는 이런 trait들의 예제이다.
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
Ord 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 {}
// 이 함수들은 오로지 이들 trait을 구현한 타입에만 유효하다.
// 사실 trait이 비어있는 것은 상관없다.
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()`는 바인드 대상으로 인해 bluejay에는 동작하지 않는다.
// 반대의 경우도 마찬가지.
println!("A cardinal is {}", red(&cardinal));
println!("A blue jay is {}", blue(&blue_jay));
//println!("A turkey is {}", red(&_turkey));
// ^ TODO: 해당 라인의 주석을 지워보세요.
}See also:
std::cmp::Eq, std::cmp::Ords, and traits