The code is taken from thread::scope, to my understanding, 'env should be late bound.
//scope::<_>(f); // function takes 2 generic arguments but 1 generic argument was supplied
shows it is late bound too, but both the scope::<'_, _, _>(f) and scope::<_, _>(f) can be compiled.
I don't know why the three generic parameters version can be compiled.
struct Scope<'a, 'b: 'a> {
a: &'a i32,
b: &'b i32,
}
fn scope<'env, F, U>(f: F) -> U
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> U,
{
let n = 0;
let s = Scope { a: &n, b: &0 };
f(&s)
}
fn f<'a, 'b>(_s: &'a Scope<'a, 'b>) -> () {
()
}
fn main() {
//scope::<_>(f); // function takes 2 generic arguments but 1 generic argument was supplied
scope::<'_, _, _>(f);
scope::<_, _>(f);
}
And, if 'env is late-bound, then the scope function can be desugar something like that:
for<'env> FnOnce(F) -> U
meaning any 'env can be used, but the following code was refused:
struct Scope<'a, 'b> {
a: &'a i32,
b: &'b i32,
}
fn scope<'env, F, U>(f: F) -> U
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> U,
{
let m = 0;
let n = 0;
let s = Scope { a: &n, b: &m };
f(&s)
}
fn f<'a, 'b>(_s: &'a Scope<'a, 'b>) -> () {
()
}
fn main() {
scope::<_, _>(f);
}