@@ -12,7 +12,7 @@ Let's start with an example.
1212``` rust
1313// Note: debug expects two parameters with the *same* lifetime
1414fn debug <'a >(a : & 'a str , b : & 'a str ) {
15- println! (" a = {:?} b = {:?}" , a , b );
15+ println! (" a = {a :?} b = {b :?}" );
1616}
1717
1818fn main () {
@@ -25,7 +25,7 @@ fn main() {
2525}
2626```
2727
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,
2929we might see the following error:
3030
3131``` text
@@ -72,12 +72,12 @@ so we need to understand how this stuff really works, and how we can mess it up.
7272Going back to our example above, we can say that ` 'static <: 'world ` .
7373For now, let's also accept the idea that subtypes of lifetimes can be passed through references
7474(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 ` .
7676With that, the example above will compile:
7777
7878``` 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 :?}" );
8181}
8282
8383fn main () {
@@ -95,7 +95,7 @@ fn main() {
9595Above, we glossed over the fact that ` 'static <: 'b ` implied that ` &'static T <: &'b T ` . This uses a property known as _ variance_ .
9696It's not always as simple as this example, though. To understand that, let's try to extend this example a bit:
9797
98- ``` rust,compile_fail
98+ ``` rust,compile_fail,E0597
9999fn assign<T>(input: &mut T, val: T) {
100100 *input = val;
101101}
@@ -106,7 +106,7 @@ fn main() {
106106 let world = String::from("world");
107107 assign(&mut hello, &world);
108108 }
109- println!("{}", hello ); // use after free 😿
109+ println!("{hello}" ); // use after free 😿
110110}
111111```
112112
@@ -177,7 +177,7 @@ For more types, see the ["Variance" section][variance-table] on the reference.
177177Now that we have some more formal understanding of variance,
178178let's go through some more examples in more detail.
179179
180- ``` rust,compile_fail
180+ ``` rust,compile_fail,E0597
181181fn assign<T>(input: &mut T, val: T) {
182182 *input = val;
183183}
@@ -188,7 +188,7 @@ fn main() {
188188 let world = String::from("world");
189189 assign(&mut hello, &world);
190190 }
191- println!("{}", hello );
191+ println!("{hello}" );
192192}
193193```
194194
@@ -230,7 +230,7 @@ This is counter to the `&T` case:
230230
231231``` rust
232232fn debug <T : std :: fmt :: Debug >(a : T , b : T ) {
233- println! (" a = {:?} b = {:?}" , a , b );
233+ println! (" a = {a :?} b = {b :?}" );
234234}
235235```
236236
0 commit comments