Borrow Checker 2 @WaffleLapkin @BoxyUwU
Warning: this quiz is still "work-in-progress", some questions might not have good explanations (or any at all), formatting/structure/titles/etc are not final, and so on. You might want to return here on a later date.
fn main() {
consume_fn(identity);
}
fn identity(x: &u32) -> &u32 {
x
}
fn consume_fn<T>(_: impl FnOnce(&u32) -> T) {}
Solution
error[E0308]: mismatched types
--> examples/borrowck_2.rs:2:5
|
2 | consume_fn(identity);
| ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected reference `&_`
found reference `&_`
note: the lifetime requirement is introduced here
--> examples/borrowck_2.rs:9:42
|
9 | fn consume_fn<T>(_: impl FnOnce(&u32) -> T) {}
| ^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `code` (example "borrowck_2") due to 1 previous error
identity
returns a reference with the same lifetime as the argument, i.e. it's for<'a> fn(&'a u32) -> &'a u32
.
consume_fn
accepts some function with a signature of for<'a> fn(&'a u32) -> T
where T
is some type.
Notably, T
is defined outside of the binder (aka "for all" qualifier, for<'a>
), so it can't name 'a
, causing the error.