派生

通过 #[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);

5. // `Inches`,可以打印的元组结构体
6. #[derive(Debug)]
7. struct Inches(i32);

9. impl Inches {
10. fn to_centimeters(&self) -> Centimeters {
11. let &Inches(inches) = self;

13. Centimeters(inches as f64 * 2.54)
14. }
15. }

17. // `Seconds`,不带附加属性的元组结构体
18. struct Seconds(i32);

20. fn main() {
21. let _one_second = Seconds(1);

23. // 报错:`Seconds` 不能打印;它没有实现 `Debug` trait
24. //println!("One second looks like: {:?}", _one_second);
25. // 试一试 ^ 将此行注释去掉

27. // 报错:`Seconds`不能比较;它没有实现 `PartialEq` trait
28. //let _this_is_true = (_one_second == _one_second);
29. // 试一试 ^ 将此行注释去掉

31. let foot = Inches(12);

33. println!("One foot equals {:?}", foot);

35. let meter = Centimeters(100.0);

37. let cmp =
38. if foot.to_centimeters() < meter {
39. "smaller"
40. } else {
41. "bigger"
42. };

44. println!("One foot is {} than one meter.", cmp);
45. }

derive