Skip to content

Commit

Permalink
begin rewriting chapter on subtyping
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate authored and ehuss committed May 28, 2023
1 parent b5f018f commit ae69217
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/subtyping.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
# Subtyping and Variance

Rust uses lifetimes to track the relationships between borrows and ownership.
However, a naive implementation of lifetimes would be either too restrictive,
or permit undefined behaviour. Let's see a few examples:

```rust,ignore
fn debug<'a>(a: &'a str, b: &'a str) {
println!("a = {:?} b = {:?}", a, b)
}
fn main() {
let a: &'static str = "hello";
{
let b = String::from("world");
let b = &b; // 'b has a shorter lifetime than 'static
debug(a, b);
}
}
```

In an overly restrictive implementation of lifetimes, since `a` and `b` have differeing lifetimes,
we might see the following error:

```text
error[E0308]: mismatched types
--> src/main.rs:6:16
|
6 | debug(a, b);
| ^
| |
| expected `&'static str`, found struct `&'b str`
```

This is over-restrictive. In this case, what we want is to accept any type that lives "at least as long" as `<'a>`.
This is what subtyping is intended to fix.

Let's define lifetime `'a` to be a `subtype` of lifetime `'b`, if and only if `'a` lives _at least as long_ as `'b`.
We will denote this as `'a: 'b`

---

Subtyping is a relationship between types that allows statically typed
languages to be a bit more flexible and permissive.

Expand Down

0 comments on commit ae69217

Please sign in to comment.