@@ -53,8 +53,8 @@ fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
53
53
type FunPtr = fn(&str) -> &str; // elided
54
54
type FunPtr = for<'a> fn(&'a str) -> &'a str; // expanded
55
55
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
58
58
```
59
59
60
60
## Default trait object lifetimes
@@ -88,45 +88,45 @@ If neither of those rules apply, then the bounds on the trait are used:
88
88
trait Foo { }
89
89
90
90
// 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>
93
93
94
94
// ...and so are these:
95
- impl Foo {}
96
- impl Foo + 'static {}
95
+ impl dyn Foo {}
96
+ impl dyn Foo + 'static {}
97
97
98
98
// ...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)
101
101
102
102
// 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>
105
105
106
106
// This is an error:
107
107
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
110
110
```
111
111
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>` .
114
114
115
115
``` rust,ignore
116
116
// For the following trait...
117
117
trait Bar<'a>: 'a { }
118
118
119
119
// ...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>
122
122
123
123
// ...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 {}
126
126
127
127
// This is still an error:
128
128
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
129
- TwoBounds<'a, 'b, Foo<'c>>
129
+ TwoBounds<'a, 'b, dyn Foo<'c>>
130
130
```
131
131
132
132
## ` 'static ` lifetime elision
@@ -162,11 +162,11 @@ usual rules, then it will error. By way of example:
162
162
const RESOLVED_SINGLE: fn(&str) -> &str = ..
163
163
164
164
// 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 = ..
166
166
167
167
// There is insufficient information to bound the return reference lifetime
168
168
// 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 = ..
170
170
```
171
171
172
172
[ closure trait ] : types.html#closure-types
0 commit comments