@@ -12,7 +12,7 @@ Let's start with an example.
12
12
``` rust
13
13
// Note: debug expects two parameters with the *same* lifetime
14
14
fn debug <'a >(a : & 'a str , b : & 'a str ) {
15
- println! (" a = {:?} b = {:?}" , a , b );
15
+ println! (" a = {a :?} b = {b :?}" );
16
16
}
17
17
18
18
fn main () {
@@ -25,7 +25,7 @@ fn main() {
25
25
}
26
26
```
27
27
28
- In a conservative implementation of lifetimes, since ` hello ` and ` world ` have differing lifetimes,
28
+ In a conservative implementation of lifetimes, since ` hello ` and ` world ` have different lifetimes,
29
29
we might see the following error:
30
30
31
31
``` text
@@ -72,12 +72,12 @@ so we need to understand how this stuff really works, and how we can mess it up.
72
72
Going back to our example above, we can say that ` 'static <: 'world ` .
73
73
For now, let's also accept the idea that subtypes of lifetimes can be passed through references
74
74
(more on this in [ Variance] ( #variance ) ),
75
- _ e.g._ ` &'static str ` is a subtype of ` &'world str ` , then we can let a ` &'static str ` "downgrade" into a ` &'world str ` .
75
+ _ e.g._ ` &'static str ` is a subtype of ` &'world str ` , then we can "downgrade" ` &'static str ` into a ` &'world str ` .
76
76
With that, the example above will compile:
77
77
78
78
``` rust
79
- fn debug <T : std :: fmt :: Debug >(a : T , b : T ) {
80
- println! (" a = {:?} b = {:?}" , a , b );
79
+ fn debug <' a >(a : & ' a str , b : & ' a str ) {
80
+ println! (" a = {a :?} b = {b :?}" );
81
81
}
82
82
83
83
fn main () {
@@ -95,7 +95,7 @@ fn main() {
95
95
Above, we glossed over the fact that ` 'static <: 'b ` implied that ` &'static T <: &'b T ` . This uses a property known as _ variance_ .
96
96
It's not always as simple as this example, though. To understand that, let's try to extend this example a bit:
97
97
98
- ``` rust,compile_fail
98
+ ``` rust,compile_fail,E0597
99
99
fn assign<T>(input: &mut T, val: T) {
100
100
*input = val;
101
101
}
@@ -106,7 +106,7 @@ fn main() {
106
106
let world = String::from("world");
107
107
assign(&mut hello, &world);
108
108
}
109
- println!("{}", hello ); // use after free 😿
109
+ println!("{hello}" ); // use after free 😿
110
110
}
111
111
```
112
112
@@ -177,7 +177,7 @@ For more types, see the ["Variance" section][variance-table] on the reference.
177
177
Now that we have some more formal understanding of variance,
178
178
let's go through some more examples in more detail.
179
179
180
- ``` rust,compile_fail
180
+ ``` rust,compile_fail,E0597
181
181
fn assign<T>(input: &mut T, val: T) {
182
182
*input = val;
183
183
}
@@ -188,7 +188,7 @@ fn main() {
188
188
let world = String::from("world");
189
189
assign(&mut hello, &world);
190
190
}
191
- println!("{}", hello );
191
+ println!("{hello}" );
192
192
}
193
193
```
194
194
@@ -230,7 +230,7 @@ This is counter to the `&T` case:
230
230
231
231
``` rust
232
232
fn debug <T : std :: fmt :: Debug >(a : T , b : T ) {
233
- println! (" a = {:?} b = {:?}" , a , b );
233
+ println! (" a = {a :?} b = {b :?}" );
234
234
}
235
235
```
236
236
0 commit comments