Skip to content

Commit

Permalink
Apply some review suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed May 28, 2023
1 parent 1517460 commit 54ca7d1
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/subtyping.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Let's start with an example.
```rust
// Note: debug expects two parameters with the *same* lifetime
fn debug<'a>(a: &'a str, b: &'a str) {
println!("a = {:?} b = {:?}", a, b);
println!("a = {a:?} b = {b:?}");
}

fn main() {
Expand All @@ -25,7 +25,7 @@ fn main() {
}
```

In a conservative implementation of lifetimes, since `hello` and `world` have differing lifetimes,
In a conservative implementation of lifetimes, since `hello` and `world` have different lifetimes,
we might see the following error:

```text
Expand Down Expand Up @@ -72,12 +72,12 @@ so we need to understand how this stuff really works, and how we can mess it up.
Going back to our example above, we can say that `'static <: 'world`.
For now, let's also accept the idea that subtypes of lifetimes can be passed through references
(more on this in [Variance](#variance)),
_e.g._ `&'static str` is a subtype of `&'world str`, then we can let a `&'static str` "downgrade" into a `&'world str`.
_e.g._ `&'static str` is a subtype of `&'world str`, then we can "downgrade" `&'static str` into a `&'world str`.
With that, the example above will compile:

```rust
fn debug<T: std::fmt::Debug>(a: T, b: T) {
println!("a = {:?} b = {:?}", a, b);
fn debug<'a>(a: &'a str, b: &'a str) {
println!("a = {a:?} b = {b:?}");
}

fn main() {
Expand All @@ -95,7 +95,7 @@ fn main() {
Above, we glossed over the fact that `'static <: 'b` implied that `&'static T <: &'b T`. This uses a property known as _variance_.
It's not always as simple as this example, though. To understand that, let's try to extend this example a bit:

```rust,compile_fail
```rust,compile_fail,E0597
fn assign<T>(input: &mut T, val: T) {
*input = val;
}
Expand All @@ -106,7 +106,7 @@ fn main() {
let world = String::from("world");
assign(&mut hello, &world);
}
println!("{}", hello); // use after free 😿
println!("{hello}"); // use after free 😿
}
```

Expand Down Expand Up @@ -177,7 +177,7 @@ For more types, see the ["Variance" section][variance-table] on the reference.
Now that we have some more formal understanding of variance,
let's go through some more examples in more detail.

```rust,compile_fail
```rust,compile_fail,E0597
fn assign<T>(input: &mut T, val: T) {
*input = val;
}
Expand All @@ -188,7 +188,7 @@ fn main() {
let world = String::from("world");
assign(&mut hello, &world);
}
println!("{}", hello);
println!("{hello}");
}
```

Expand Down Expand Up @@ -230,7 +230,7 @@ This is counter to the `&T` case:

```rust
fn debug<T: std::fmt::Debug>(a: T, b: T) {
println!("a = {:?} b = {:?}", a, b);
println!("a = {a:?} b = {b:?}");
}
```

Expand Down

0 comments on commit 54ca7d1

Please sign in to comment.