简体   繁体   English

在impl块上不需要在通用参数上绑定生命周期

[英]Lifetime bound on generic parameter not required on impl block

I have a simple Wrapper that holds a reference to a slice of type T . 我有一个简单的Wrapper,其中包含对类型T的切片的引用。 My first attempt looked like this: 我的第一次尝试是这样的:

struct SliceWrapper<'a, T> {
    a: &'a [T],
}

Now the compiler rightly complains that T may not live long enough. 现在,编译器正确地抱怨T寿命可能不够长。 So I do what it suggests and add a lifetime bound to T . 因此,我按照建议的方式进行操作,并为T添加了生命周期。 Thereby telling the compiler that all borrowed content within T outlives 'a . 从而告诉编译器T中所有借用的内容都超过了'a

This is how my final attempt looks: 这是我最后一次尝试的样子:

#[derive(Debug)]
struct SliceWrapper<'a, T: 'a> {
    a: &'a [T],
}

impl<'a, T> SliceWrapper<'a, T> {
    fn new(n: &'a [T]) -> SliceWrapper<'a, T> {
        SliceWrapper { a: n }
    }
}

fn main() {
    let array = [1, 2, 3, 4, 5];

    let aw = SliceWrapper::new(&array[..2]);

    println!("{:?}", aw);
}

This works. 这可行。 But, if I changed the impl to 但是,如果我将展示次数更改为

impl<'a, T: 'a> SliceWrapper<'a, T> {
    fn new(n: &'a [T]) -> SliceWrapper<'a, T> {
        SliceWrapper { a: n }
    }
}

it also works. 它也可以。 Why don't I have to also specify the lifetime bound on the impl block? 为什么不用在impl块上也指定生存期限制? What is different from my first successful attempt where I omitted it on the impl block? 与我在impl块上省略它的第一次成功尝试有什么不同?

I would suggest that defining 我建议定义

struct SliceWrapper<'a, T: 'a>

would implicitly force the bound T: 'a wherever you use SliceWrapper . 会隐式强制绑定T: 'a无论您T: 'a何处使用SliceWrapper So both are equivalent: 因此,两者是等效的:

 impl<'a, T>     SliceWrapper<'a, T> // T: 'a is "contained" in SliceWrapper definition
 impl<'a, T: 'a> SliceWrapper<'a, T> // but it doesn't harm to say it again

This is just a feeling, not a well-proven assertion (I didn't check RFCs or compiler code). 这只是一种感觉,而不是经过充分证明的断言(我没有检查RFC或编译器代码)。 It would be consistent with what happens with type inference: you usually have to supply the type once, and can be implicit in most other places. 这将与类型推断发生的情况一致:您通常必须提供一次类型,并且在大多数其他地方都可以隐式提供。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有生命周期参数的特征作为通用界限的问题 - Trouble with using a trait with lifetime parameter as a generic bound Blanket impl,HRTB和“ Impl”抽象返回类型:“期望的绑定生存期参数” - Blanket impl, HRTB, and “impl” abstract return type: “expected bound lifetime parameter” 尝试调用泛型函数时出现“预期的绑定生命周期参数”错误 - “expected bound lifetime parameter” error when attempting to call a generic function 如何以及何时在 Rust 的 impl 块中使用泛型类型参数? - How and when to use the generic type parameter in impl block in Rust? 预期的约束寿命参数,找到具体的寿命 - Expected bound lifetime parameter, found concrete lifetime 在 Rust 中,当对作为通用参数传递的值进行装箱时,为什么需要“静态”生命周期限制? - In Rust, when boxing a value passed as a generic argument, why is a `'static` lifetime bound required? 如何在结构本身不需要泛型参数的通用 impl 块中添加类型注释? - How to add type annotations to generic impl block where the struct itself doesn't require a generic parameter? 将泛型参数与 impl 的另一个泛型参数匹配 - Matching a generic parameter to another generic parameter of an impl 为什么这个 Rust 代码编译时在结构上有生命周期绑定,但如果绑定仅在 impl 上,则会给出生命周期错误? - Why does this Rust code compile with a lifetime bound on the struct, but give a lifetime error if the bound is only on the impl? 在impl块或方法上指定特征绑定是否更好? - Is it better to specify trait bound on the impl block or on the method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM