通过例子学习Rust

30.1 衍生(deriving)

The compiler is capable of providing basic implementations for some traits via the #[deriving] attribute. These traits can still be manually implemented if a more complex behavior is required.

// A tuple struct that can be compared #[deriving(PartialEq, PartialOrd)] struct Centimeters(f64); // A tuple struct that can be printed #[deriving(Show)] struct Inches(int); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } // A vanilla tuple struct struct Seconds(int); fn main() { let _one_second = Seconds(1); // Error! `Seconds` can't be printed, because it doesn't implement the // `Show` trait //println!("One second looks like: {}", _one_second); // TODO ^ Try uncommenting this line // Error: `Seconds` can't be compared, because it doesn't implement the // `PartialEq` trait //let _this_is_true = _one_second == _one_second; // TODO ^ Try uncommenting this line let foot = Inches(12); println!("One foot === {}", foot); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("one foot is {} than one meter", cmp); }

This is a list of the "derivable" traits:

  • Comparison traits: Eq, PartialEq, Ord, PartialOrd
  • Serialization: Encodable, Decodable
  • Clone, to create T from &T via a copy.
  • Hash, to compute a hash from &T.
  • Rand, to create a random instance of a data type.
  • Default, to create an empty instance of a data type.
  • Zero, to create a zero instance of a numeric data type.
  • FromPrimitive, to create an instance from a numeric primitive.
  • Show, to format a value using the {} formatter.