Skip to content

Commit 2e5c339

Browse files
committed
Use dyn in lifetime elision chapter
1 parent 42c4696 commit 2e5c339

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/lifetime-elision.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
5353
type FunPtr = fn(&str) -> &str; // elided
5454
type FunPtr = for<'a> fn(&'a str) -> &'a str; // expanded
5555
56-
type FunTrait = Fn(&str) -> &str; // elided
57-
type FunTrait = for<'a> Fn(&'a str) -> &'a str; // expanded
56+
type FunTrait = dyn Fn(&str) -> &str; // elided
57+
type FunTrait = dyn for<'a> Fn(&'a str) -> &'a str; // expanded
5858
```
5959

6060
## Default trait object lifetimes
@@ -88,45 +88,45 @@ If neither of those rules apply, then the bounds on the trait are used:
8888
trait Foo { }
8989
9090
// These two are the same as Box<T> has no lifetime bound on T
91-
Box<Foo>
92-
Box<Foo + 'static>
91+
Box<dyn Foo>
92+
Box<dyn Foo + 'static>
9393
9494
// ...and so are these:
95-
impl Foo {}
96-
impl Foo + 'static {}
95+
impl dyn Foo {}
96+
impl dyn Foo + 'static {}
9797
9898
// ...so are these, because &'a T requires T: 'a
99-
&'a Foo
100-
&'a (Foo + 'a)
99+
&'a dyn Foo
100+
&'a (dyn Foo + 'a)
101101
102102
// std::cell::Ref<'a, T> also requires T: 'a, so these are the same
103-
std::cell::Ref<'a, Foo>
104-
std::cell::Ref<'a, Foo + 'a>
103+
std::cell::Ref<'a, dyn Foo>
104+
std::cell::Ref<'a, dyn Foo + 'a>
105105
106106
// This is an error:
107107
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
108-
TwoBounds<'a, 'b, Foo> // Error: the lifetime bound for this object type cannot
109-
// be deduced from context
108+
TwoBounds<'a, 'b, dyn Foo> // Error: the lifetime bound for this object type
109+
// cannot be deduced from context
110110
```
111111

112-
Note that the innermost object sets the bound, so `&'a Box<Foo>` is still `&'a
113-
Box<Foo + 'static>`.
112+
Note that the innermost object sets the bound, so `&'a Box<dyn Foo>` is still
113+
`&'a Box<dyn Foo + 'static>`.
114114

115115
```rust,ignore
116116
// For the following trait...
117117
trait Bar<'a>: 'a { }
118118
119119
// ...these two are the same:
120-
Box<Bar<'a>>
121-
Box<Bar<'a> + 'a>
120+
Box<dyn Bar<'a>>
121+
Box<dyn Bar<'a> + 'a>
122122
123123
// ...and so are these:
124-
impl<'a> Foo<'a> {}
125-
impl<'a> Foo<'a> + 'a {}
124+
impl<'a> dyn Foo<'a> {}
125+
impl<'a> dyn Foo<'a> + 'a {}
126126
127127
// This is still an error:
128128
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
129-
TwoBounds<'a, 'b, Foo<'c>>
129+
TwoBounds<'a, 'b, dyn Foo<'c>>
130130
```
131131

132132
## `'static` lifetime elision
@@ -162,11 +162,11 @@ usual rules, then it will error. By way of example:
162162
const RESOLVED_SINGLE: fn(&str) -> &str = ..
163163
164164
// Resolved as `Fn<'a, 'b, 'c>(&'a Foo, &'b Bar, &'c Baz) -> usize`.
165-
const RESOLVED_MULTIPLE: &Fn(&Foo, &Bar, &Baz) -> usize = ..
165+
const RESOLVED_MULTIPLE: &dyn Fn(&Foo, &Bar, &Baz) -> usize = ..
166166
167167
// There is insufficient information to bound the return reference lifetime
168168
// relative to the argument lifetimes, so this is an error.
169-
const RESOLVED_STATIC: &Fn(&Foo, &Bar) -> &Baz = ..
169+
const RESOLVED_STATIC: &dyn Fn(&Foo, &Bar) -> &Baz = ..
170170
```
171171

172172
[closure trait]: types.html#closure-types

0 commit comments

Comments
 (0)