Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 9, 2022
1 parent 80d84bc commit fcad3e1
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/trait-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,21 @@ Rust sometimes infers some bounds the user would have otherwise been required to
```rust
fn requires_t_outlives_a<'a, T>(x: &'a T) {}
```
While this function requires `t` to outlive `'a`, this is inferred as the function signature
While this function requires `T` to outlive `'a`, this is inferred because the function signature
contains the type `&'a T` which is only valid if `T: 'a` holds.

Rust adds implied bounds for all inputs and outputs of functions. Inside of `requires_t_outlives_a`
you can assume `T: 'a` to hold even if you don't explicitly specify this:
```rust
fn requires_t_outlives_a<'a, T>(x: &'a T) {
// This compiles, because `T: 'a` is implied by
// the reference type `&'a T`.
requires_t_outlives_a_not_implied::<'a, T>();
}

fn not_implied<'a, T>() {
// This errors, because `T: 'a` is not implied by
// the function signature.
requires_t_outlives_a_not_implied::<'a, T>();
}

Expand Down

0 comments on commit fcad3e1

Please sign in to comment.