Skip to content

Commit 55d9464

Browse files
committed
Move derive example into an example block
1 parent 324bc6b commit 55d9464

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/attributes/derive.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ r[attributes.derive]
44
r[attributes.derive.intro]
55
The *`derive` attribute* allows new [items] to be automatically generated for data structures.
66

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`:
9+
>
10+
> ```rust
11+
> #[derive(PartialEq, Clone)]
12+
> struct Foo<T> {
13+
> a: i32,
14+
> b: T,
15+
> }
16+
> ```
17+
>
18+
> The generated `impl` for `PartialEq` is equivalent to
19+
>
20+
> ```rust
21+
> # struct Foo<T> { a: i32, b: T }
22+
> impl<T: PartialEq> PartialEq for Foo<T> {
23+
> fn eq(&self, other: &Foo<T>) -> bool {
24+
> self.a == other.a && self.b == other.b
25+
> }
26+
> }
27+
> ```
28+
729
r[attributes.derive.syntax]
830
It uses the [MetaListPaths] syntax to specify a list of traits to implement or paths to [derive macros] to process.
931
10-
For example, the following 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`:
11-
12-
```rust
13-
#[derive(PartialEq, Clone)]
14-
struct Foo<T> {
15-
a: i32,
16-
b: T,
17-
}
18-
```
19-
20-
The generated `impl` for `PartialEq` is equivalent to
21-
22-
```rust
23-
# struct Foo<T> { a: i32, b: T }
24-
impl<T: PartialEq> PartialEq for Foo<T> {
25-
fn eq(&self, other: &Foo<T>) -> bool {
26-
self.a == other.a && self.b == other.b
27-
}
28-
}
29-
```
3032
3133
r[attributes.derive.proc-macro]
3234
You can implement `derive` for your own traits through [procedural macros].

0 commit comments

Comments
 (0)