Skip to content

生命周期HRTB相关的问题 #14

Open
@Mota-Link

Description

@Mota-Link

生命周期中的HRTB,概括地说,应该用于哪些场景呢?
我知道在如下的场景中:

fn foo<'a,T>(f: T)
where
     T: Fn(&'a i32) -> &'a i32,
{
    let x = 0;
    f(&x);   //error: `x` does not live long enough
}

应该使用HRTB:

fn foo<T>(f: T)
where
    for<'a> T: Fn(&'a i32) -> &'a i32,
{
    let x = 0;
    f(&x);
}

但似乎省略该生命周期标注,也能有相同的效果:

fn foo<T>(f: T)
where
    T: Fn(&i32) -> &i32,
{
    let x = 0;
    f(&x);
}

所以使用HRTB的意义是什么呢?还是说,当省略闭包的生命周期参数时,隐式使用了HRTB来推导?求解惑

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions