[英]How to specify a temporary lifetime when specifying trait bounds?
I would like to declare a struct that wraps a generic type T
like this:我想声明一个包装通用类型T
的结构,如下所示:
use std::ops::Add;
struct MyStruct<T> where T: Add<&T, Output=T> {
t: T
}
This fails with:这失败了:
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
How can I tell the compiler that &T
may be a temporary variable, and therefore any lifetime is ok?我如何告诉编译器&T
可能是一个临时变量,因此任何生命周期都可以?
I don't want to change my struct signature to MyStruct<'a, T>
, as that makes the usage more verbose and complicated.我不想将我的结构签名更改为MyStruct<'a, T>
,因为这会使用法更加冗长和复杂。
use std::ops::Add;
struct MyStruct<T> where T: for <'a> Add<&'a T, Output=T> {
t: T
}
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=864f6c2ad80544adfa7da96cef8eb69c操场: https : //play.rust-lang.org/? version = stable & mode = debug & edition = 2018 & gist =864f6c2ad80544adfa7da96cef8eb69c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.