@@ -1425,6 +1425,8 @@ names are effectively reserved. Some significant attributes include:
1425
1425
* The ` test ` attribute, for marking functions as unit tests.
1426
1426
* The ` allow ` , ` warn ` , ` forbid ` , and ` deny ` attributes, for controlling lint checks. Lint checks supported
1427
1427
by the compiler can be found via ` rustc -W help ` .
1428
+ * The ` deriving ` attribute, for automatically generating
1429
+ implementations of certain traits.
1428
1430
1429
1431
Other attributes may be added or removed during development of the language.
1430
1432
@@ -1526,6 +1528,47 @@ A complete list of the built-in language items follows:
1526
1528
> ** Note:** This list is likely to become out of date. We should auto-generate it
1527
1529
> from ` librustc/middle/lang_items.rs ` .
1528
1530
1531
+ ### Deriving
1532
+
1533
+ The ` deriving ` attribute allows certain traits to be automatically
1534
+ implemented for data structures. For example, the following will
1535
+ create an ` impl ` for the ` Eq ` and ` Clone ` traits for ` Foo ` , the type
1536
+ parameter ` T ` will be given the ` Eq ` or ` Clone ` constraints for the
1537
+ appropriate ` impl ` :
1538
+
1539
+ ~~~
1540
+ #[deriving(Eq, Clone)]
1541
+ struct Foo<T> {
1542
+ a: int,
1543
+ b: T
1544
+ }
1545
+ ~~~
1546
+
1547
+ The generated ` impl ` for ` Eq ` is equivalent to
1548
+
1549
+ ~~~
1550
+ # struct Foo<T> { a: int, b: T }
1551
+ impl<T: Eq> Eq for Foo<T> {
1552
+ fn eq(&self, other: &Foo<T>) -> bool {
1553
+ self.a == other.a && self.b == other.b
1554
+ }
1555
+
1556
+ fn ne(&self, other: &Foo<T>) -> bool {
1557
+ self.a != other.a || self.b != other.b
1558
+ }
1559
+ }
1560
+ ~~~
1561
+
1562
+ Supported traits for ` deriving ` are:
1563
+
1564
+ * Comparison traits: ` Eq ` , ` TotalEq ` , ` Ord ` , ` TotalOrd ` .
1565
+ * Serialization: ` Encodable ` , ` Decodable ` . These require ` std ` .
1566
+ * ` Clone ` , to perform deep copies.
1567
+ * ` IterBytes ` , to iterate over the bytes in a data type.
1568
+ * ` Rand ` , to create a random instance of a data type.
1569
+ * ` ToStr ` , to convert to a string. For a type with this instance,
1570
+ ` obj.to_str() ` has the same output as ` fmt!("%?", obj) ` .
1571
+
1529
1572
# Statements and expressions
1530
1573
1531
1574
Rust is _ primarily_ an expression language. This means that most forms of
0 commit comments