[英]What are sum of Rust types + lifetime?
There's a trait that does this:有一个特点可以做到这一点:
pub trait Device<'a> {
type RxToken: RxToken + 'a;
type TxToken: TxToken + 'a;
What dos this mean?这是什么意思? Why not
type RxToken: RxToken<'a>
?为什么不
type RxToken: RxToken<'a>
?
Let's see an example:让我们看一个例子:
trait SayHello {
fn say_hello(&self);
}
pub trait Device<'a> {
type MyType : SayHello + 'a;
}
As you see, SayHello
itself does not know anything about lifetime constraints, but MyType
is constrained to satisfy SayHello
and must be valid for lifetime 'a
.如您所见,
SayHello
本身对生命周期约束一无所知,但MyType
被约束以满足SayHello
并且必须在生命周期'a
内有效。
In contrast, consider相反,考虑
trait SayHello {
fn say_hello(&self);
}
pub trait Device2<'a> {
type MyType : SayHello;
}
Here, Device2
has no lifetime requirements for MyType
- it only needs to satisfy SayHello
.在这里,
Device2
对MyType
没有生命周期要求——它只需要满足SayHello
即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.