You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/attributes/derive.md
+58-31Lines changed: 58 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,66 @@ r[attributes.derive]
2
2
# Derive
3
3
4
4
r[attributes.derive.intro]
5
-
The *`derive` attribute* allows new [items] to be automatically generated for
6
-
data structures.
5
+
The *`derive`[attribute][attributes]* invokes one or more [derive macros], allowing new [items] to be automatically generated for data structures. You can create `derive` macros with [procedural macros].
6
+
7
+
> [!EXAMPLE]
8
+
> The [`PartialEq`][macro@PartialEq] derive macro emits an [implementation] of [`PartialEq`] for `Foo<T> where T: PartialEq`. The [`Clone`][macro@Clone] derive macro does likewise for [`Clone`].
9
+
>
10
+
> ```rust
11
+
> #[derive(PartialEq, Clone)]
12
+
> structFoo<T> {
13
+
> a:i32,
14
+
> b:T,
15
+
> }
16
+
> ```
17
+
>
18
+
> Thegenerated `impl` itemsareequivalentto:
19
+
>
20
+
> ```rust
21
+
> # structFoo<T> { a:i32, b:T }
22
+
> impl<T:PartialEq> PartialEqforFoo<T> {
23
+
> fneq(&self, other:&Foo<T>) ->bool {
24
+
> self.a ==other.a &&self.b ==other.b
25
+
> }
26
+
> }
27
+
>
28
+
> impl<T:Clone> CloneforFoo<T> {
29
+
> fnclone(&self) ->Self {
30
+
> Foo { a:self.a.clone(), b:self.b.clone() }
31
+
> }
32
+
> }
33
+
> ```
7
34
8
35
r[attributes.derive.syntax]
9
-
It uses the [MetaListPaths] syntax to specify a list of
10
-
traits to implement or paths to [derive macros] to process.
11
-
12
-
For example, the following will create an [`impl` item] for the
13
-
[`PartialEq`] and [`Clone`] traits for `Foo`, and the type parameter `T` will be
14
-
given the `PartialEq` or `Clone` constraints for the appropriate `impl`:
15
-
16
-
```rust
17
-
#[derive(PartialEq, Clone)]
18
-
structFoo<T> {
19
-
a:i32,
20
-
b:T,
21
-
}
22
-
```
23
-
24
-
The generated `impl` for `PartialEq` is equivalent to
25
-
26
-
```rust
27
-
# structFoo<T> { a:i32, b:T }
28
-
impl<T:PartialEq> PartialEqforFoo<T> {
29
-
fneq(&self, other:&Foo<T>) ->bool {
30
-
self.a ==other.a &&self.b ==other.b
31
-
}
32
-
}
33
-
```
34
-
35
-
r[attributes.derive.proc-macro]
36
-
You can implement `derive` for your own traits through [procedural macros].
36
+
The `derive` attributeusesthe [MetaListPaths] syntaxtospecifyalistofpathsto [derivemacros] toinvoke.
37
+
38
+
r[attributes.derive.allowed-positions]
39
+
The `derive` attributemaybeappliedto [structs][items.struct], [enums][items.enum], and [unions][items.union].
40
+
41
+
r[attributes.derive.duplicates]
42
+
The `derive` attributemaybespecifiedmultipletimesonanitem, withallderivemacroslistedinallattributesbeinginvoked.
43
+
44
+
r[attributes.derive.stdlib]
45
+
The `derive` attributeisexportedinthestandardlibrarypreludeas [`core::prelude::v1::derive`].
0 commit comments