方法 `mul` 具有不兼容的 trait 类型 [英] Method `mul` has an incompatible type for trait

查看:35
本文介绍了方法 `mul` 具有不兼容的 trait 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rust 中创建一个简单的矩阵结构,我正在尝试实现一些基本的运算符方法:

I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods:

use std::ops::Mul;

struct Matrix {
    cols: i32,
    rows: i32,
    data: Vec<f32>,
}

impl Matrix {
    fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix {
        Matrix {
            cols: cols,
            rows: rows,
            data: data,
        }
    }
}

impl Mul<f32> for Matrix {
    type Output = Matrix;

    fn mul(&self, m: f32) -> Matrix {
        let mut new_data = Vec::with_capacity(self.cols * self.rows);

        for i in 0..self.cols * self.rows {
            new_data[i] = self.data[i] * m;
        }

        return Matrix {
            cols: *self.cols,
            rows: *self.rows,
            data: new_data,
        };
    }
}

fn main() {}

我仍在熟悉 Rust 和系统编程,我确信错误非常明显.编译器告诉我:

I'm still familiarizing myself with Rust and systems programming and I'm sure the error is pretty obvious. The compiler tells me:

error[E0053]: method `mul` has an incompatible type for trait
  --> src/main.rs:22:5
   |
22 |     fn mul(&self, m: f32) -> Matrix {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Matrix`, found &Matrix
   |
   = note: expected type `fn(Matrix, f32) -> Matrix`
              found type `fn(&Matrix, f32) -> Matrix`

它指的是 for 循环的内容(我相信).我试过玩其他一些东西,但我无法理解.

It's referring to the contents of the for loop (I believe). I've tried playing around with a few other things but I can't get my head around it.

推荐答案

错误信息在这里:

error[E0053]: method `mul` has an incompatible type for trait
  --> src/main.rs:22:5
   |
22 |     fn mul(&self, m: f32) -> Matrix {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Matrix`, found &Matrix
   |
   = note: expected type `fn(Matrix, f32) -> Matrix`
              found type `fn(&Matrix, f32) -> Matrix`

让我们看看 Mul trait 以了解您的实现为何不匹配:

Let's look at the Mul trait to see why your implementation doesn't match:

pub trait Mul<RHS = Self> {
    type Output;
    fn mul(self, rhs: RHS) -> Self::Output;
}

这表示除非您进一步指定,RHS 将与 Self 的类型相同.Self 是将在其上实现 trait 的类型.让我们看看你的定义:

This says that unless you specify anything further, RHS will be the same type as Self. Self is the type that the trait will be implemented on. Let's look at your definition:

impl Mul<f32> for Matrix {
    type Output = Matrix;

    fn mul(&self, m: f32) -> Matrix {}
}

在您的情况下,您已将 f32 替换为 RHS,并将 Matrix 替换为 Output.此外,Matrix 是实现类型.让我们采用 trait 定义并替换进去,生成一些伪 Rust:

In your case, you have substituted f32 for RHS, and Matrix for Output. Also, Matrix is the implementing type. Let's take the trait definition and substitute in, producing some pseudo-Rust:

pub trait Mul {
    fn mul(self, rhs: f32) -> Matrix;
}

现在你看到有什么不同了吗?

Now do you see what is different?

// Trait
fn mul(self,  m: f32) -> Matrix;
// Your implementation
fn mul(&self, m: f32) -> Matrix;

您错误地指定使用 &self 而不是 self.

You have incorrectly specified that you take &self instead of self.

为了完整起见,这里是实现.我免费进行了样式修复!

For completeness, here's the implementation. I threw in style fixes at no charge!

impl Mul<f32> for Matrix {
    type Output = Matrix;

    fn mul(self, m: f32) -> Matrix {
        let new_data = self.data.into_iter().map(|v| v * m).collect();

        Matrix {
            cols: self.cols,
            rows: self.rows,
            data: new_data,
        }
    }
}

这有点低效,因为它会释放和重新分配 data 向量.由于您是按值获取 Matrix,我们可以就地编辑它:

This is a bit inefficient as it deallocates and reallocates the data vector. Since you are taking the Matrix by value, we can just edit it in place:

impl Mul<f32> for Matrix {
    type Output = Matrix;

    fn mul(mut self, m: f32) -> Matrix {
        for v in &mut self.data {
            *v *= m
        }

        self
    }
}

这篇关于方法 `mul` 具有不兼容的 trait 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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