要求在通用函数中实现Mul [英] Requiring implementation of Mul in generic function

查看:86
本文介绍了要求在通用函数中实现Mul的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Rust中实现一个通用函数,其中对于参数的唯一要求是应该定义乘法操作。我试图实现一个通用的权力,但会用简单的立方体函数来说明问题:

 使用std :: ops :: Mul; 

fn立方体< T:Mul>(x:T) - > T {
x * x * x
}

fn main(){
println!(5 ^ 3 = {},cube(5));
}

编译时出现这个错误:

error [E0369]:二元运算`*`不能用于< T as std :: ops :: Mul> ::输出`
- > src / main.rs:4:5
|
4 | x * x * x
| ^^^^^^^^^
|
=注意:< T as std :: ops :: Mul> :: Output`

这是什么意思?我选择了错误的特质吗?如何解决这个问题?

解决方案

让我们分析一下您的示例:

  fn cube< T:Mul>(x:T) - > T {
let a = x * x;
让b = a * x;
b

a的类型是什么 b ?在这种情况下, a 的类型是< T as std :: ops :: Mul> :: Output - 听起来很熟悉错误信息?然后,我们再次尝试将该类型乘以 x ,但不能保证 Output 能够相乘我们做最简单的事情,并说 T * T 需要产生一个<$ c $ c> T :

  fn cube< T:Mul< Output = T>>( x:T) - > T {
x * x * x
}

不幸的是,错误:
$ b

  error [E0382]:移动值的使用:`x` 
- > src / lib.rs:6:9
|
6 | x * x * x
| - 移动
|后使用的^值|
|价值移到这里
|
= note:移动是因为`x`的类型为'T`,它没有实现`Copy`特性

这是因为 Mul trait接受值的参数,所以我们添加 Copy ,以便我们可以复制这些值。



我也切换到,其中子句我更喜欢它,并且使用这种内联很难操作:

  fn cube< T>(x:T) - > T 
其中
T:Mul<输出= T> +复制
{
x * x * x
}


I'm trying to implement a generic function in Rust where the only requirement for the argument is that the multiplication operation should be defined. I'm trying to implement a generic "power", but will go with a simpler cube function to illustrate the problem:

use std::ops::Mul;

fn cube<T: Mul>(x: T) -> T {
    x * x * x
}

fn main() {
    println!("5^3 = {}", cube(5));
}

When compiling I get this error:

error[E0369]: binary operation `*` cannot be applied to type `<T as std::ops::Mul>::Output`
 --> src/main.rs:4:5
  |
4 |     x * x * x
  |     ^^^^^^^^^
  |
  = note: an implementation of `std::ops::Mul` might be missing for `<T as std::ops::Mul>::Output`

What does this mean? Did I choose the wrong trait? How can I resolve this?

解决方案

Let's break down your example a bit:

fn cube<T: Mul>(x: T) -> T {
    let a = x * x;
    let b = a * x;
    b
}

What are the types of a and b? In this case, the type of a is <T as std::ops::Mul>::Output — sound familiar from the error message? Then, we are trying to multiply that type by x again, but there's no guarantee that Output is able to be multiplied by anything!

Let's do the simplest thing and say that T * T needs to result in a T:

fn cube<T: Mul<Output = T>>(x: T) -> T {
    x * x * x
}

Unfortunately, this gives two similar errors:

error[E0382]: use of moved value: `x`
 --> src/lib.rs:6:9
  |
6 |     x * x * x
  |     -   ^ value used here after move
  |     |
  |     value moved here
  |
  = note: move occurs because `x` has type `T`, which does not implement the `Copy` trait

Which is because the Mul trait takes arguments by value, so we add the Copy so we can duplicate the values.

I also switched to the where clause as I like it better and it is unwieldy to have that much inline:

fn cube<T>(x: T) -> T
where
    T: Mul<Output = T> + Copy
{
    x * x * x
}

这篇关于要求在通用函数中实现Mul的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆