2016-11-01 63 views
-1

this issue page for Rust,它給出了core::num::bignum::FullOps以下示例代碼:如何添加尺寸超級特徵到鏽性狀?

pub trait FullOps { 
    ... 
    fn full_mul(self, other: Self, carry: Self) -> (Self /*carry*/, Self); 
    ... 
} 

然後,它說:

這裏full_mul返回(Self, Self)元組中的功能,這是隻有 合式當Self -type爲Sized - 由於那個和其他原因, 當SelfSized時,這個特徵纔有意義。在這個案例和大多數其他案例中的解決方案是添加缺失的Sized超小型。

如何添加缺少的Sized supertrait?

回答

2

這很簡單:改變第一行:

pub trait FullOps : Sized { 

Playground link

4

「超級特徵」 只是一種束縛,真的。

您可以在特徵級別或方法級別放置邊界。在這裏,建議您將其放置在水平的特質:

pub trait FullOps: Sized { 
    fn full_mul(self, other: Self, carry: Self) -> (Self, Self); 
} 

其他的解決辦法是將其放置在方法級別:

pub trait FullOps { 
    fn full_mul(self, other: Self, carry: Self) -> (Self, Self) 
     where Self: Sized; 
}