指定特征边界时如何指定临时生命周期?
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
}
我想像这样声明一个包装泛型类型的结构 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
}