@@ -51,8 +51,8 @@ fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
51
51
type FunPtr = fn(&str) -> &str; // elided
52
52
type FunPtr = for<'a> fn(&'a str) -> &'a str; // expanded
53
53
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
56
56
```
57
57
58
58
## Default trait object lifetimes
@@ -86,45 +86,45 @@ If neither of those rules apply, then the bounds on the trait are used:
86
86
trait Foo { }
87
87
88
88
// 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>
91
91
92
92
// ...and so are these:
93
- impl Foo {}
94
- impl Foo + 'static {}
93
+ impl dyn Foo {}
94
+ impl dyn Foo + 'static {}
95
95
96
96
// ...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)
99
99
100
100
// 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>
103
103
104
104
// This is an error:
105
105
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
108
108
```
109
109
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>` .
112
112
113
113
``` rust,ignore
114
114
// For the following trait...
115
115
trait Bar<'a>: 'a { }
116
116
117
117
// ...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>
120
120
121
121
// ...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 {}
124
124
125
125
// This is still an error:
126
126
struct TwoBounds<'a, 'b, T: ?Sized + 'a + 'b>
127
- TwoBounds<'a, 'b, Foo<'c>>
127
+ TwoBounds<'a, 'b, dyn Foo<'c>>
128
128
```
129
129
130
130
## ` 'static ` lifetime elision
@@ -160,11 +160,11 @@ usual rules, then it will error. By way of example:
160
160
const RESOLVED_SINGLE: fn(&str) -> &str = ..
161
161
162
162
// 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 = ..
164
164
165
165
// There is insufficient information to bound the return reference lifetime
166
166
// 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 = ..
168
168
```
169
169
170
170
[ closure trait ] : types.html#closure-types
0 commit comments