You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/subtyping.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,16 @@ However, a naive implementation of lifetimes would be either too restrictive,
5
5
or permit undefined behavior.
6
6
7
7
In order to allow flexible usage of lifetimes
8
-
while also preventing mis-use, Rust uses a combination of **Subtyping** and **Variance**.
8
+
while also preventing their misuse, Rust uses a combination of **Subtyping** and **Variance**.
9
9
10
10
## Subtyping
11
11
12
12
Subtyping is the idea that one type can be a *subtype* of another.
13
-
Let's define that `A: B` is equivalent to saying '`A` is a subtype of `B`'.
14
-
What this is suggesting to us is that the set of *requirements* that `B` defines
15
-
are completely satisfied by `A`. `A` may then have more requirements.
13
+
14
+
Let's define that `Sub` is a subtype of `Super` (we'll be using the notation `Sub: Super` throughout this chapter)
15
+
16
+
What this is suggesting to us is that the set of *requirements* that `Super` defines
17
+
are completely satisfied by `Sub`. `Sub` may then have more requirements.
16
18
17
19
An example of simple subtyping that exists in the language are [supertraits](https://doc.rust-lang.org/stable/book/ch19-03-advanced-traits.html?highlight=supertraits#using-supertraits-to-require-one-traits-functionality-within-another-trait)
18
20
@@ -72,7 +74,7 @@ Now that we have a defined set of requirements for lifetimes, we can define how
72
74
`'a` may define a region larger than `'b`, but that still fits our definition.
73
75
Going back to our example above, we can say that `'static: 'b`.
74
76
75
-
For now, let's accept the idea that subtypes of lifetimes can be transitive (more on this in [Variance](#variance>)),
77
+
For now, let's accept the idea that subtypes of lifetimes can be transitive (more on this in [Variance](#variance)),
76
78
eg. `&'static str` is a subtype of `&'b str`, then we can let them coerce, and then the example above will compile
0 commit comments