繁体   English   中英

指定特征边界时如何指定临时生命周期?

[英]How to specify a temporary lifetime when specifying trait bounds?

我想声明一个包装通用类型T的结构,如下所示:

use std::ops::Add;
struct MyStruct<T> where T: Add<&T, Output=T> {
    t: T
}

这失败了:

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/lib.rs:3:33
  |
3 | struct MyStruct<T> where T: Add<&T, Output=T> {
  |                                 ^ explicit lifetime name needed here

error[E0310]: the parameter type `T` may not live long enough

我如何告诉编译器&T可能是一个临时变量,因此任何生命周期都可以?

我不想将我的结构签名更改为MyStruct<'a, T> ,因为这会使用法更加冗长和复杂。

use std::ops::Add;
struct MyStruct<T> where T: for <'a> Add<&'a T, Output=T> {
    t: T
}

操场: https : //play.rust-lang.org/? version = stable & mode = debug & edition = 2018 & gist =864f6c2ad80544adfa7da96cef8eb69c

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM