当我们只有通用特征时,如何处理恒定数学的锈蚀方法是什么
这是我损坏的示例代码。请注意,我只想将值加倍,并允许任何知道如何倍增的东西。
fn by_two<T: Mul<Output = T>>(x: T) -> T {
x * 2
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您遇到的问题是您需要说出您正在使用的内容。
2
默认为&lt; integer&gt;
,如果未指定的将其计数为i32
,那么这里就是这样的示例:另外,您可以离开结果直至特征。通常 *等于仅使用
x * 2
而无需调用函数。但是,这与大多数类型都无法使用,因为Rust通常希望原语将乘以同类类型的其他私有。我们可以将其更改为从&lt; i32&gt; 使用
以使其更具包容性。
在这一点上,您可能会注意到问题。我们需要弄清楚哪种类型的“ 2”是可以用
t
倍增的。来自&lt; i32&gt;
解决方案适用于原始类型,但是如果您尝试将矢量或矩阵插入该功能,则将遇到困难。一种解决方案是添加一个单独的通用,以允许用户指定其乘以的内容。但是,这开始变得有些麻烦。与以前的版本不同,我们不能让编译器推断出一种用于
k
的类型,因为可能有多种类型可以满足k
的要求。一个不能完全解决我们的问题的选项,但可能会有所帮助的是使用像 Num-Thraits 。坦白地说,这实际上与示例2相同,但更广泛地概念为2。
您有很多选择,但没有一个包容性。如果您想保持匹配尽可能通用,最好先编写功能,然后确定值必须满足的数学属性才能使其正常工作。您可以使用
num-traits
对于基本特征界(例如在浮子和INT之间。然后,对于更复杂的概念,我建议使用alga
。
I think the issue you are running into is you needs to say what you are multiplying with.
2
defaults to an<integer>
which if unspecified counts as ani32
, so here is that example:Alternatively, you could just leave the result up to the trait. This is generally* equivalent to just using
x * 2
without calling the function.However, this won't work with most types since Rust usually expects primitives to be multiplied with other privatives of the same type. We could change it to use
From<i32>
to make it more inclusive.At this point you may be noticing the problem. We need to figure out what type "2" is so it can multiply with
T
. TheFrom<i32>
solution works well for primitive types, but you will start to run into difficulties if you tried to plug a vector or matrix into the function. One solution would be to add a separate generic to allow the user to specify what it gets multiplied with.However, this is starting to get a bit cumbersome. Unlike the previous versions, we can't let the compiler infer a type to use for
K
since there might be multiple types which could satisfy the requirements put onK
.One option which doesn't quite solve our problem, but may help us a bit would be to use a crate like
num-traits
. Though to be honest, this really just amounts to more or less the same thing as example 2, but with a more generalized concept of 2.You have a ton of options, but none are all inclusive. If you want to keep your match as generic as possible, you are probably best off writing the function first, then determining what mathematical properties the values must satisfy to make it work. You can use
num-traits
for basic trait bounds such as differentiating between floats and ints. Then for more complex concepts, I recommend usingalga
.好吧... 是两个?例如,如果我在
color
上将乘法定义为混合两种颜色,那么将颜色乘以两种颜色意味着什么?解决此问题的最简单方法是选择一个非常小的数据类型(例如
u8
)并接受任何可以(尝试)从<<<代码> u8 :越通用(但麻烦)的方法是使用
num
板条箱,并接受任何可以乘以的东西,并且具有的概念
一个
,使用一个 +一个
作为两个。Well... what is two? For example, if I define my multiplication on
Color
as mixing two colors, then what does it mean to multiply a color by two?The easiest way to solve this is by choosing a very small data type that two fits in (such as
u8
) and accepting anyT
which can (try to) convert fromu8
:The more general (but cumbersome) way is to use the
num
crate, and accept anything that can be multiplied, and has a concept ofOne
, usingOne + One
as two.