派生

通过 #[derive] 属性,编译器能够提供一些对于 trait 的基本实现。如果需要一个更复杂的业务,这些 trait 仍然可以手动实现。(原文:The compiler is capable of providing basic implementations for some traits via the #[derive] attribute. These traits can still be manually implemented if a more complex behavior is required.)

下面列举了 “derivable”(可派生的)trait:

  • 比较 trait:
    Eq, PartialEq, Ord, PartialOrd
  • Clone, 采用复制(copy)方式从 &T 创建 T
  • Copy,给出“复制语义”(’copy semantics’)来替代“移动语义”(’move semantics’)。
  • Hash,从 &T 计算哈希值(hash)。
  • Default, 创建数据类型的一个空实例。
  • Zero,创建数字数据类型的一个零值实例(zero instance)。
  • Debug,使用 {:?} 格式化程序(formatter)格式化一个值。
  1. // `Centimeters`,可以比较的元组结构体
  2. #[derive(PartialEq, PartialOrd)]
  3. struct Centimeters(f64);
  4. // `Inches`,可以打印的元组结构体
  5. #[derive(Debug)]
  6. struct Inches(i32);
  7. impl Inches {
  8. fn to_centimeters(&self) -> Centimeters {
  9. let &Inches(inches) = self;
  10. Centimeters(inches as f64 * 2.54)
  11. }
  12. }
  13. // `Seconds`,不带附加属性的元组结构体
  14. struct Seconds(i32);
  15. fn main() {
  16. let _one_second = Seconds(1);
  17. // 报错:`Seconds` 不能打印;它没有实现 `Debug` trait
  18. //println!("One second looks like: {:?}", _one_second);
  19. // 试一试 ^ 将此行注释去掉
  20. // 报错:`Seconds`不能比较;它没有实现 `PartialEq` trait
  21. //let _this_is_true = (_one_second == _one_second);
  22. // 试一试 ^ 将此行注释去掉
  23. let foot = Inches(12);
  24. println!("One foot equals {:?}", foot);
  25. let meter = Centimeters(100.0);
  26. let cmp =
  27. if foot.to_centimeters() < meter {
  28. "smaller"
  29. } else {
  30. "bigger"
  31. };
  32. println!("One foot is {} than one meter.", cmp);
  33. }

derive