我想声明一个包装如下通用类型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
}