[英]How to add a Sized supertrait to a Rust trait?
At this issue page for Rust , it gives the following example code for core::num::bignum::FullOps
: 在Rust的问题页面上 ,它给出了core::num::bignum::FullOps
的以下示例代码:
pub trait FullOps {
...
fn full_mul(self, other: Self, carry: Self) -> (Self /*carry*/, Self);
...
}
Then it says that: 然后说:
Here the function
full_mul
returns a(Self, Self)
tuple, which is only well-formed when theSelf
-type isSized
- for that and other reasons, the trait only makes sense whenSelf
isSized
. 此处,函数full_mul
返回一个(Self, Self)
元组,该元组仅在将Self
类型设置为Sized
时才具有正确的格式-出于该原因和其他原因,该特性仅在Self
处于Sized
时才有意义。 The solution in this case and most others is to add the missingSized
supertrait. 在这种情况下以及其他大多数情况下,解决方案是添加缺少的Sized
超特征。
How does one add the missing Sized
supertrait? 一个如何添加缺失的Sized
特征?
A "super trait" is just a bound, really. 实际上,“超级特质”仅仅是一个界限。
You can place a bound either at trait level or method level. 您可以在特征级别或方法级别放置界限。 Here, you are advised to place it at trait level: 在这里,建议您将其放置在特征级别:
pub trait FullOps: Sized {
fn full_mul(self, other: Self, carry: Self) -> (Self, Self);
}
The other solution would be to place it at method level: 另一种解决方案是将其置于方法级别:
pub trait FullOps {
fn full_mul(self, other: Self, carry: Self) -> (Self, Self)
where Self: Sized;
}
It's quite simple: change the first line to: 这很简单:将第一行更改为:
pub trait FullOps : Sized {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.