Skip to content

Commit f52b2d9

Browse files
committed
Use dyn in lifetime elision chapter
1 parent 6fe62a5 commit f52b2d9

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
@@ -51,8 +51,8 @@ fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
5151
type FunPtr = fn(&str) -> &str; // elided
5252
type FunPtr = for<'a> fn(&'a str) -> &'a str; // expanded
5353
54-
type FunTrait = Fn(&str) -> &str; // elided
55-
type FunTrait = for<'a> Fn(&'a str) -> &'a str; // expanded
54+
type FunTrait = dyn Fn(&str) -> &str; // elided
55+
type FunTrait = dyn for<'a> Fn(&'a str) -> &'a str; // expanded
5656
```
5757

5858
## Default trait object lifetimes
@@ -86,45 +86,45 @@ If neither of those rules apply, then the bounds on the trait are used:
8686
trait Foo { }
8787
8888
// These two are the same as Box<T> has no lifetime bound on T
89-
Box<Foo>
90-
Box<Foo + 'static>
89+
Box<dyn Foo>
90+
Box<dyn Foo + 'static>
9191
9292
// ...and so are these:
93-
impl Foo {}
94-
impl Foo + 'static {}
93+
impl dyn Foo {}
94+
impl dyn Foo + 'static {}
9595
9696
// ...so are these, because &'a T requires T: 'a
97-
&'a Foo
98-
&'a (Foo + 'a)
97+
&'a dyn Foo
98+
&'a (dyn Foo + 'a)
9999
100100
// std::cell::Ref<'a, T> also requires T: 'a, so these are the same
101-
std::cell::Ref<'a, Foo>
102-
std::cell::Ref<'a, Foo + 'a>
101+
std::cell::Ref<'a, dyn Foo>
102+
std::cell::Ref<'a, dyn Foo + 'a>
103103
104104
// This is an error:
105105
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
106-
TwoBounds<'a, 'b, Foo> // Error: the lifetime bound for this object type cannot
107-
// be deduced from context
106+
TwoBounds<'a, 'b, dyn Foo> // Error: the lifetime bound for this object type
107+
// cannot be deduced from context
108108
```
109109

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

113113
```rust,ignore
114114
// For the following trait...
115115
trait Bar<'a>: 'a { }
116116
117117
// ...these two are the same:
118-
Box<Bar<'a>>
119-
Box<Bar<'a> + 'a>
118+
Box<dyn Bar<'a>>
119+
Box<dyn Bar<'a> + 'a>
120120
121121
// ...and so are these:
122-
impl<'a> Foo<'a> {}
123-
impl<'a> Foo<'a> + 'a {}
122+
impl<'a> dyn Foo<'a> {}
123+
impl<'a> dyn Foo<'a> + 'a {}
124124
125125
// This is still an error:
126126
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
127-
TwoBounds<'a, 'b, Foo<'c>>
127+
TwoBounds<'a, 'b, dyn Foo<'c>>
128128
```
129129

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

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

0 commit comments

Comments
 (0)