简体   繁体   English

如何在Rust特征中添加Sized超级特征?

[英]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 the Self -type is Sized - for that and other reasons, the trait only makes sense when Self is Sized . 此处,函数full_mul返回一个(Self, Self)元组,该元组仅在将Self类型设置为Sized时才具有正确的格式-出于该原因和其他原因,该特性仅在Self处于Sized时才有意义。 The solution in this case and most others is to add the missing Sized 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 {

Playground link 游乐场链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为特征的引用指定一个超特征? - How to specify a supertrait for the reference of a trait? 如何在 Rust 中将超特征向下转换为子特征 - How to Downcast a Supertrait to a SubTrait in Rust 为什么要求AddAssign作为超级特征的特征也要求它是大小的? - Why does a trait requiring AddAssign as a supertrait also require it to be Sized? 如何创建一个指定超特征关联类型的特征? - How to create a trait that specifies the associated type of a supertrait? 在Rust中,如何修复错误“特性`core :: types :: Sized`没有为`Object +&#39;a`类型实现” - In Rust, how can I fix the error “the trait `core::kinds::Sized` is not implemented for the type `Object+'a`” 仅当自我:大小时才具有超级特质 - Supertrait only when Self : Sized trait upcast coercion workaround 是如何工作的(获取 supertrait 实例的 trait 方法)? - How does the trait upcast coercion workaround work (trait method for getting an instance of the supertrait)? 如何将特征与使用特征的关联类型作为参数的超特征绑定? - How do I bound a trait with a supertrait that uses the trait's associated type as a parameter? 什么是特性`core :: types :: Sized`没有实现生锈中的类型`<generic#0>`? - What is the trait `core::kinds::Sized` is not implemented for the type `<generic #0>` in rust? 如何使用特征默认方法中的BorrowMut超特征访问结构域? - How do I use a BorrowMut supertrait to access struct fields in a trait default method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM