# What is a good mental model of borrow checker?

**URL:** <https://users.rust-lang.org/t/what-is-a-good-mental-model-of-borrow-checker/86806>\
**Category:** help\
**Created:** [January 1, 2023, 4:46am UTC](https://users.rust-lang.org/t/what-is-a-good-mental-model-of-borrow-checker/86806 "2023-01-01T04:46:26Z")\
**Posts on this page:** 1\
**Showing post:** 53

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [January 8, 2023, 9:13am UTC](https://users.rust-lang.org/t/what-is-a-good-mental-model-of-borrow-checker/86806/53 "2023-01-08T09:13:10Z")

</div>

> [@adal](#):
>
> Can you explain a bit about your example?

Yes, you're drilling into the heart of it. Let's compare:

```rust
fn take_hr_closure< F: Fn(& I) -> O, I, O>(f: F) { let _ignored = f; }
fn take_closure<'a, F: Fn(&'a I) -> O, I: 'a, O>(f: F) { let _ignored = f; }

```

The first says that `F` must accept _any lifetime_ \[1\] and produce the same output, `O`, for any lifetime. It can be called with any lifetime; the caller of `take_hr_closure` has no say in what lifetimes it will be called with. The lifetimes have no lower bound.

Incidentally, a desugared version would be

```rust
fn take_hr_closure<F: for<'any> Fn(&'any I) -> O, I, O>(f: F)

```

 

and `F` is a higher-ranked type.

The second says that `F` must accept _one specific lifetime_ (`'a`) of the caller's choosing. The lifetime does have a lower bound: it's impossible for the caller of `take_closure` to choose a lifetime shorter than the call to `take_closure`.

These are very different things! Instead of ignoring the closures... [let's use them!](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ffc287dd8800ecba02da279a01dea9f4)

```rust
fn take_hr_closure< F: Fn(& I) -> O, I: Default, O>(f: F) {
    let i = I::default();
    f(&i);
}

fn take_closure<'a, F: Fn(&'a I) -> O, I: Default + 'a, O>(f: F) {
    let i = I::default();
    f(&i);
}

```

The second does not compile because of the aforementioned lower bound. [The contrast is even more stark for `&mut` / `&'a mut`.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c763528e7c9609f92d0205372a84b518)

[Here's a comment that drove this home for me.](https://github.com/rust-lang/rust/issues/44224#issuecomment-330058902) It's a bit dense.

* * *

> [@adal](#):
>
> Can you tell more and give a few examples?

[Here's an example with some brief explanation at the top.](https://users.rust-lang.org/t/problems-matching-up-lifetimes-between-various-traits-and-closure-parameters/71994/7) It was written before GAT stabilized, but in brief this pattern:

```rust
trait Indirect<'a> {
    type Arg: 'a + FromValue<'a>;
}
// Or sometimes
trait Indirect<'a, _Lifetime_Guard = &'a Self> {
    type Arg: 'a + FromValue<'a>;
}
// Elsewhere
where T: for<'a> Indirect<'a>

```

are GAT simulations of

```rust
trait Indirect {
    type Arg<'a>: FromValue<'a>;
}
// Or
trait Indirect {
    type Arg<'a>: FromValue<'a> where Self: 'a;
}
// Elsewhere
where T: Indirect

```

I have a cleaner playground version in my drafts somewhere.

* * *

1. that is well-formed, i.e., cannot be outside of `I`'s validity

---

_[View the full topic](https://users.rust-lang.org/t/what-is-a-good-mental-model-of-borrow-checker/86806)._
