Skip to content

Commit b6d4a49

Browse files
authored
add a high level explanation, and remove a disclaimer (rust-lang#1982)
1 parent dbfb44d commit b6d4a49

5 files changed

+39
-20
lines changed

src/SUMMARY.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@
125125
- [Which `ParamEnv` do I use?](./param_env/param_env_acquisition.md)
126126
- [Type inference](./type-inference.md)
127127
- [Trait solving](./traits/resolution.md)
128-
- [Early and Late Bound Parameter Definitions](./early-late-bound-summary.md)
129-
- [What are early and late bound parameters](./what-does-early-late-bound-mean.md)
130-
- [Interactions with turbofishing](./turbofishing-and-early-late-bound.md)
128+
- [Early and Late Bound Parameter Definitions](./early-late-bound-params/early-late-bound-summary.md)
129+
- [Implementation nuances of early/late bound parameters](./early-late-bound-params/early-late-bound-implementation-nuances.md)
130+
- [Interactions with turbofishing](./early-late-bound-params/turbofishing-and-early-late-bound.md)
131131
- [Higher-ranked trait bounds](./traits/hrtb.md)
132132
- [Caching subtleties](./traits/caching.md)
133133
- [Implied bounds](./traits/implied-bounds.md)

src/what-does-early-late-bound-mean.md src/early-late-bound-params/early-late-bound-implementation-nuances.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
# Early and Late Bound Parameter Definitions
1+
# Early and Late Bound Parameter Implementation Nuances
22

33
Understanding this page likely requires a rudimentary understanding of higher ranked
44
trait bounds/`for<'a>`and also what types such as `dyn for<'a> Trait<'a>` and
55
`for<'a> fn(&'a u32)` mean. Reading [the nomincon chapter](https://doc.rust-lang.org/nomicon/hrtb.html)
66
on HRTB may be useful for understanding this syntax. The meaning of `for<'a> fn(&'a u32)`
77
is incredibly similar to the meaning of `T: for<'a> Trait<'a>`.
88

9-
If you are looking for information on the `RegionKind` variants `ReLateBound` and `ReEarlyBound`
10-
you should look at the section on [bound vars and params](./bound-vars-and-params.md). This section
11-
discusses what makes generic parameters on functions and closures late/early bound. Not the general
12-
concept of bound vars and generic parameters which `RegionKind` has named somewhat confusingly
13-
with this topic.
14-
159
## What does it mean for parameters to be early or late bound
1610

1711
All function definitions conceptually have a ZST (this is represented by `TyKind::FnDef` in rustc).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Early/Late bound parameters
2+
3+
This section discusses what it means for generic parameters to be early or late bound.
4+
5+
```rust
6+
fn foo<'a, T>(b: &'a T) -> &'a T { b }
7+
// ^^ ^early bound
8+
// ^^
9+
// ^^late bound
10+
```
11+
12+
Generally when referring to an item with generic parameters you must specify a list of generic arguments corresponding to the item's generic parameters. In
13+
some cases it is permitted to elide these arguments but still, implicitly, a set of arguments are provided (i.e. `Vec::default()` desugars to `Vec::<_>::default()`).
14+
15+
For functions this is not necessarily the case, for example if we take the function `foo` from the example above and write the following code:
16+
```rust
17+
fn main() {
18+
let f = foo::<_>;
19+
20+
let b = String::new();
21+
let c = String::new();
22+
23+
f(&b);
24+
drop(b);
25+
f(&c);
26+
}
27+
```
28+
29+
This code compiles perfectly fine even though there is no single lifetime that could possibly be specified in `foo::<_>` that would allow for both
30+
the `&b` and `&c` borrows to be used as arguments (note: the `drop(b)` line forces the `&b` borrow to be shorter than the `&c` borrow). This works because
31+
the `'a` lifetime is _late bound_.
32+
33+
A generic parameter being late bound means that when we write `foo::<_>` we do not actually provide an argument for that parameter, instead we wait until _calling_ the function to provide the generic argument. In the above example this means that we are doing something like `f::<'_>(&b);` and `f::<'_>(&c);` (although in practice we do not actually support turbofishing late bound parameters in this manner)
34+
35+
It may be helpful to think of "early bound parameter" or "late bound parameter" as meaning "early provided parameter" and "late provided parameter", i.e. we provide the argument to the parameter either early (when naming the function) or late (when calling it).

src/early-late-bound-summary.md

-10
This file was deleted.

0 commit comments

Comments
 (0)