diff options
| author | Lorenz <lorenzb@student.ethz.ch> | 2015-05-30 14:25:58 +0200 |
|---|---|---|
| committer | Lorenz <lorenzb@student.ethz.ch> | 2015-05-30 14:25:58 +0200 |
| commit | a2b61e16b13226ba6ae3cecce9752eee6a6999aa (patch) | |
| tree | 521b74d250a6e145af6f78ae310855447d8832fd /src/doc | |
| parent | 621a10e7f32d790c39a0b4528369cf7959dd7d34 (diff) | |
| download | rust-a2b61e16b13226ba6ae3cecce9752eee6a6999aa.tar.gz rust-a2b61e16b13226ba6ae3cecce9752eee6a6999aa.zip | |
Extend rust reference with a section about subtyping
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/reference.md | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index 8d1b93ce3c8..68c17c04079 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -3608,6 +3608,26 @@ The notation `&self` is a shorthand for `self: &Self`. In this case, in the impl, `Self` refers to the value of type `String` that is the receiver for a call to the method `make_string`. +## Subtyping + +Subtyping is implicit and can occur at any stage in type checking or +inference. Subtyping in Rust is very restricted and occurs only due to +variance with respect to lifetimes and between types with higher ranked +lifetimes. If we were to erase lifetimes from types, then the only subtyping +would be due to type equality. + +Consider the following example: string literals always have `'static` +lifetime. Nevertheless, we can assign `s` to `t`: + +``` +fn bar<'a>() { + let s: &'static str = "hi"; + let t: &'a str = s; +} +``` +Since `'static` "lives longer" than `'a`, `&'static str` is a subtype of +`&'a str`. + # Special traits Several traits define special evaluation behavior. |
