File tree 1 file changed +29
-0
lines changed
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -492,3 +492,32 @@ If we forget to implement `Foo`, Rust will tell us:
492
492
``` text
493
493
error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]
494
494
```
495
+
496
+ # Deriving
497
+
498
+ Implementing traits like ` Debug ` and ` Default ` over and over again can become
499
+ quite tedious. For that reason, Rust provides an [ attribute] [ attributes ] that
500
+ allows you to let Rust automatically implement traits for you:
501
+
502
+ ``` rust
503
+ #[derive(Debug )]
504
+ struct Foo ;
505
+
506
+ fn main () {
507
+ println! (" {:?}" , Foo );
508
+ }
509
+ ```
510
+
511
+ [ attributes ] : attributes.html
512
+
513
+ However, deriving is limited to a certain set of traits:
514
+
515
+ - [ ` Clone ` ] ( ../core/clone/trait.Clone.html )
516
+ - [ ` Copy ` ] ( ../core/marker/trait.Copy.html )
517
+ - [ ` Debug ` ] ( ../core/fmt/trait.Debug.html )
518
+ - [ ` Default ` ] ( ../core/default/trait.Default.html )
519
+ - [ ` Eq ` ] ( ../core/cmp/trait.Eq.html )
520
+ - [ ` Hash ` ] ( ../core/hash/trait.Hash.html )
521
+ - [ ` Ord ` ] ( ../core/cmp/trait.Ord.html )
522
+ - [ ` PartialEq ` ] ( ../core/cmp/trait.PartialEq.html )
523
+ - [ ` PartialOrd ` ] ( ../core/cmp/trait.PartialOrd.html )
You can’t perform that action at this time.
0 commit comments