gpt4 book ai didi

rust - 如何将 Sized super 特征添加到 Rust 特征?

转载 作者:行者123 更新时间:2023-11-29 08:22:27 24 4
gpt4 key购买 nike

this issue page for Rust ,它为 core::num::bignum::FullOps 提供了以下示例代码:

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

然后它说:

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. The solution in this case and most others is to add the missing Sized supertrait.

如何添加缺失的 Sized 超特征?

最佳答案

“ super 特质”只是一个界限,真的。

您可以在特征级别或方法级别设置界限。这里建议放在trait级别:

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;
}

关于rust - 如何将 Sized super 特征添加到 Rust 特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com