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
Let's be a bit more clear about the purpose of the `derive` attribute
being to invoke derive macros, and let's talk about "invoking" these
macros rather than "processing" them.
Copy file name to clipboardExpand all lines: src/attributes/derive.md
+12-6Lines changed: 12 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,10 @@ r[attributes.derive]
2
2
# Derive
3
3
4
4
r[attributes.derive.intro]
5
-
The *`derive`[attribute][attributes]*allows new [items] to be automatically generated for data structures. You can implement custom `derive` macros through[procedural macros].
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
6
7
7
> [!EXAMPLE]
8
-
> The following example will create an [`impl` item] for the [`PartialEq`]and [`Clone`] traits for `Foo`, and the type parameter `T` will be given the `PartialEq` or `Clone` constraints for the appropriate `impl`:
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
9
>
10
10
> ```rust
11
11
> #[derive(PartialEq, Clone)]
@@ -15,7 +15,7 @@ The *`derive` [attribute][attributes]* allows new [items] to be automatically ge
15
15
> }
16
16
> ```
17
17
>
18
-
> Thegenerated `impl` for `PartialEq` isequivalentto
18
+
> Thegenerated `impl` itemsareequivalentto:
19
19
>
20
20
> ```rust
21
21
> # structFoo<T> { a:i32, b:T }
@@ -24,16 +24,22 @@ The *`derive` [attribute][attributes]* allows new [items] to be automatically ge
24
24
> self.a ==other.a &&self.b ==other.b
25
25
> }
26
26
> }
27
+
>
28
+
> impl<T:Clone> CloneforFoo<T> {
29
+
> fnclone(&self) ->Self {
30
+
> Foo { a:self.a.clone(), b:self.b.clone() }
31
+
> }
32
+
> }
27
33
> ```
28
34
29
35
r[attributes.derive.syntax]
30
-
The `derive` attributeusesthe [MetaListPaths] syntaxtospecifyalistofpathsto [derivemacros] toprocess.
36
+
The `derive` attributeusesthe [MetaListPaths] syntaxtospecifyalistofpathsto [derivemacros] toinvoke.
31
37
32
38
r[attributes.derive.allowed-positions]
33
39
The `derive` attributemaybeappliedto [structs][items.struct], [enums][items.enum], and [unions][items.union].
34
40
35
41
r[attributes.derive.duplicates]
36
-
The `derive` attributemaybespecifiedmultipletimesonanitem, withallentriesfromallattributesbeingprocessed.
42
+
The `derive` attributemaybespecifiedmultipletimesonanitem, withallderivemacroslistedinallattributesbeinginvoked.
37
43
38
44
r[attributes.derive.stdlib]
39
45
The `derive` attributeisexportedinthestandardlibrarypreludeas [`core::prelude::v1::derive`].
@@ -65,9 +71,9 @@ The *`automatically_derived` attribute* is automatically added to
0 commit comments