What does `<'_>` mean in the following code

fn say_some<'a, 'b>(name: String) -> impl Fn(&'a str, &'b str) {
    move |text, t2| println!("{name} 说: {text}")
fn f<'a, 'b>(s1: &'a str, s2: &'b str){

fn f1<'a: 'b, 'b>(s1: &'a str, s2: &'b str){

fn main(){
    let func = say_some::<'static, 'static>("dddd".into());
    // late bound
    let a = f;
    // the MIR ==> 4 = f(_5, _6) -> [return: bb3, unwind: bb6];
    f("222", "3333");
    // early bound
    let a1 = f1;
    // the MIR ==> _7 = f1::<'_, '_>(_8, _9) -> [return: bb4, unwind: bb6];
    f1("222", "3333");

When a lifetime parameter ('a) is early bound, it acts on a function definition (TyKind::FnDef(I::DefId, I::GenericArgs) in rustc). I think the instance of FnDef is only in this lifetime ('a )efficient.

As shown in the above code, this function calls (f1::<', '>) early boud lifetime, but anonymous lifetime.

What does this anonymous lifetime mean? How about scope? e.g. std::thread::scope 'env

// Function std::thread::scope
pub fn scope<'env, F, T>(f: F) -> T
    F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T,
1 Like

In the context of a function body, it means "compiler, infer the lifetimes for me". It's generally no different than not specifying the lifetime at all, when possible -- such as when calling a generic function, or when annotating let x: &str = ..., for example.

Intrafunction lifetimes don't have names, so letting the compiler infer the lifetime is often the only possible or sensible choice in this context.

Do you have a specific question about the function? The scope infrastructure utilizes inferred bounds in clever and subtle ways, but there's no <'_> here, and I'm not sure what you're asking.

the ‘env of the function scope should be early bound? It yet a anonymous lifetime?

It is early bound, in this case because it's part of a where clause. If you can specify it in a turbofish, it's early bound, and you can.

I'm not sure what you mean when you say "it's an anonymous lifetime".

1 Like

On a side note, please link cross-posts you made on other sites to your OP to avoid duplicated effort by the community:


Hello. :link:


OP has deleted their question on SO then.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.