Skip to content

Commit 54ca7d1

Browse files
committed
Apply some review suggestions.
1 parent 1517460 commit 54ca7d1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/subtyping.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Let's start with an example.
1212
```rust
1313
// Note: debug expects two parameters with the *same* lifetime
1414
fn debug<'a>(a: &'a str, b: &'a str) {
15-
println!("a = {:?} b = {:?}", a, b);
15+
println!("a = {a:?} b = {b:?}");
1616
}
1717

1818
fn 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,
2929
we 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.
7272
Going back to our example above, we can say that `'static <: 'world`.
7373
For 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`.
7676
With 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

8383
fn main() {
@@ -95,7 +95,7 @@ fn main() {
9595
Above, we glossed over the fact that `'static <: 'b` implied that `&'static T <: &'b T`. This uses a property known as _variance_.
9696
It'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
9999
fn 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.
177177
Now that we have some more formal understanding of variance,
178178
let's go through some more examples in more detail.
179179

180-
```rust,compile_fail
180+
```rust,compile_fail,E0597
181181
fn 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
232232
fn 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

Comments
 (0)