特性 trait

trait 是对未知类型定义的方法集:Self。它们可以访问同一个 trait 中定义的方法。

对任何数据类型实现 trait 都是可行的。在下面例子中,我们定义了包含一系列方法的 Animal。然后针对 Sheep 数据类型实现 Animal trait,允许使用来自带有 SheepAnimal 的方法(原文:allowing the use of methods from Animal with a Sheep)。

  1. struct Sheep { naked: bool, name: &'static str }
  2. trait Animal {
  3. // 静态方法标记;`Self` 表示实现者类型(implementor type)。
  4. fn new(name: &'static str) -> Self;
  5. // 实例方法(instance method)标记;这些方法将返回一个字符串。
  6. fn name(&self) -> &'static str;
  7. fn noise(&self) -> &'static str;
  8. // trait 可以提供默认方法定义(method definition)。
  9. fn talk(&self) {
  10. println!("{} says {}", self.name(), self.noise());
  11. }
  12. }
  13. impl Sheep {
  14. fn is_naked(&self) -> bool {
  15. self.naked
  16. }
  17. fn shear(&mut self) {
  18. if self.is_naked() {
  19. // 实现者(implementor)可以使用实现者的 trait 方法。
  20. println!("{} is already naked...", self.name());
  21. } else {
  22. println!("{} gets a haircut!", self.name);
  23. self.naked = true;
  24. }
  25. }
  26. }
  27. // 对 `Sheep` 实现 `Animal` trait。
  28. impl Animal for Sheep {
  29. // `Self` 是该实现者类型:`Sheep`。
  30. fn new(name: &'static str) -> Sheep {
  31. Sheep { name: name, naked: false }
  32. }
  33. fn name(&self) -> &'static str {
  34. self.name
  35. }
  36. fn noise(&self) -> &'static str {
  37. if self.is_naked() {
  38. "baaaaah?"
  39. } else {
  40. "baaaaah!"
  41. }
  42. }
  43. // 默认 trait 方法可以重载。
  44. fn talk(&self) {
  45. // 例如完们可以增加一些安静的沉思(quiet contemplation)。
  46. println!("{} pauses briefly... {}", self.name, self.noise());
  47. }
  48. }
  49. fn main() {
  50. // 这种情况需要类型标注。
  51. let mut dolly: Sheep = Animal::new("Dolly");
  52. // 试一试 ^ 移除类型标注。
  53. dolly.talk();
  54. dolly.shear();
  55. dolly.talk();
  56. }