Testcase: empty bounds

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.

  1. struct Cardinal;
  2. struct BlueJay;
  3. struct Turkey;
  4. trait Red {}
  5. trait Blue {}
  6. impl Red for Cardinal {}
  7. impl Blue for BlueJay {}
  8. // These functions are only valid for types which implement these
  9. // traits. The fact that the traits are empty is irrelevant.
  10. fn red<T: Red>(_: &T) -> &'static str { "red" }
  11. fn blue<T: Blue>(_: &T) -> &'static str { "blue" }
  12. fn main() {
  13. let cardinal = Cardinal;
  14. let blue_jay = BlueJay;
  15. let _turkey = Turkey;
  16. // `red()` won't work on a blue jay nor vice versa
  17. // because of the bounds.
  18. println!("A cardinal is {}", red(&cardinal));
  19. println!("A blue jay is {}", blue(&blue_jay));
  20. //println!("A turkey is {}", red(&_turkey));
  21. // ^ TODO: Try uncommenting this line.
  22. }

See also:

std::cmp::Eq, std::cmp::Ords, and traits