Skip to content

Commit 66e72b9

Browse files
authored
Merge pull request #39 from Mark-Simulacrum/dyn-trait-impl-trait
Use dyn Trait over the old syntax
2 parents 017141e + 9dbd6e9 commit 66e72b9

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/2018/transitioning/traits/dyn-trait.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ is sometimes slower, and often cannot be used at all when its alternatives can.
3434

3535
Furthermore, with `impl Trait` arriving, "`impl Trait` vs `dyn Trait`" is much
3636
more symmetric, and therefore a bit nicer, than "`impl Trait` vs `Trait`".
37+
`impl Trait` is explained further in the next section.
3738

3839
In the new edition, you should therefore prefer `dyn Trait` to just `Trait`
3940
where you need a trait object.

src/2018/transitioning/traits/impl-trait.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ trait Trait {}
5454

5555
impl Trait for i32 {}
5656

57-
fn returns_a_trait_object() -> Box<Trait> {
57+
fn returns_a_trait_object() -> Box<dyn Trait> {
5858
Box::new(5)
5959
}
6060
```
6161

6262
However, this has some overhead: the `Box<T>` means that there's a heap
63-
allocation here, and this will use dynamic dispatch. But we only ever
64-
return one possible thing here, the `Box<i32>`. This means that we're
65-
paying for dynamic dispatch, even though we don't use it!
63+
allocation here, and this will use dynamic dispatch. See the [`dyn Trait`] section
64+
for an explanation of this syntax. But we only ever return one possible thing
65+
here, the `Box<i32>`. This means that we're paying for dynamic dispatch, even
66+
though we don't use it!
6667

6768
With `impl Trait`, the code above could be written like this:
6869

@@ -82,6 +83,8 @@ we still can obscure the `i32` return type.
8283
With `i32`, this isn't super useful. But there's one major place in Rust
8384
where this is much more useful: closures.
8485

86+
[`dyn Trait`]: /2018/transitioning/traits/dyn-trait.html
87+
8588
### `impl Trait` and closures
8689

8790
> If you need to catch up on closures, check out [their chapter in the
@@ -92,7 +95,7 @@ family of traits, however. This means that previously, the only way to return
9295
a closure from a function was to use a trait object:
9396

9497
```rust
95-
fn returns_closure() -> Box<Fn(i32) -> i32> {
98+
fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
9699
Box::new(|x| x + 1)
97100
}
98101
```

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
- [Feature status](2018/status.md)
1212
- [Transitioning to Rust 2018](2018/transitioning/to-rust-2018.md)
1313
- [Trait system](2018/transitioning/traits/index.md)
14-
- [`impl Trait`](2018/transitioning/traits/impl-trait.md)
1514
- [`dyn Trait`](2018/transitioning/traits/dyn-trait.md)
15+
- [`impl Trait`](2018/transitioning/traits/impl-trait.md)
1616
- [Module system](2018/transitioning/modules/index.md)
1717
- [Path clarity](2018/transitioning/modules/path-clarity.md)
1818
- [Macro changes](2018/transitioning/modules/macros.md)

0 commit comments

Comments
 (0)