Skip to content

Commit a2b61e1

Browse files
committed
Extend rust reference with a section about subtyping
1 parent 621a10e commit a2b61e1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/doc/reference.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,6 +3608,26 @@ The notation `&self` is a shorthand for `self: &Self`. In this case,
36083608
in the impl, `Self` refers to the value of type `String` that is the
36093609
receiver for a call to the method `make_string`.
36103610

3611+
## Subtyping
3612+
3613+
Subtyping is implicit and can occur at any stage in type checking or
3614+
inference. Subtyping in Rust is very restricted and occurs only due to
3615+
variance with respect to lifetimes and between types with higher ranked
3616+
lifetimes. If we were to erase lifetimes from types, then the only subtyping
3617+
would be due to type equality.
3618+
3619+
Consider the following example: string literals always have `'static`
3620+
lifetime. Nevertheless, we can assign `s` to `t`:
3621+
3622+
```
3623+
fn bar<'a>() {
3624+
let s: &'static str = "hi";
3625+
let t: &'a str = s;
3626+
}
3627+
```
3628+
Since `'static` "lives longer" than `'a`, `&'static str` is a subtype of
3629+
`&'a str`.
3630+
36113631
# Special traits
36123632

36133633
Several traits define special evaluation behavior.

0 commit comments

Comments
 (0)