Rust 实现特征时类型不匹配
为了学习生锈,我正在建立自己的矩阵类。我对Add trait的实现如下所示:Rust 实现特征时类型不匹配,rust,Rust,为了学习生锈,我正在建立自己的矩阵类。我对Add trait的实现如下所示: impl<T: Add> Add for Matrix<T> { type Output = Matrix<T>; fn add(self, _rhs: Matrix<T>) -> Matrix<T> { assert!(self.rows == _rhs.rows && self.cols ==
impl<T: Add> Add for Matrix<T>
{
type Output = Matrix<T>;
fn add(self, _rhs: Matrix<T>) -> Matrix<T>
{
assert!(self.rows == _rhs.rows && self.cols == _rhs.cols,
"attempting to add matrices of different sizes");
let mut res: Matrix<T> = Matrix::<T>{
rows: self.rows,
cols: self.cols,
data : Vec::<T>::with_capacity(self.rows * self.cols),
};
for i in 0..self.rows*self.cols{
res.data.push(self.data[i] + _rhs.data[i]);
}
res
}
}
impl为矩阵添加
{
类型输出=矩阵;
fn添加(自身,矩阵)->矩阵
{
断言!(self.rows==\u rhs.rows&&self.cols==\u rhs.cols,
“试图添加不同大小的矩阵”);
让mut res:Matrix=Matrix::{
行:self.rows,
科尔斯:赛尔夫·科尔斯,
数据:Vec:::具有_容量(self.rows*self.cols),
};
对于0..self.rows*self.cols中的i{
res.data.push(self.data[i]+_rhs.data[i]);
}
物件
}
}
但是我得到了以下错误
Compiling matrix v0.1.0 (file://~/soft/rust/projects/matrix)
src/lib.rs:35:27: 35:54 error: mismatched types:
expected `T`,
found `<T as core::ops::Add>::Output`
(expected type parameter,
found associated type) [E0308]
src/lib.rs:35 res.data.push(self.data[i] + _rhs.data[i]);
^~~~~~~~~~~~~~~~~~~~~~~~~~~
编译矩阵v0.1.0(文件://~/soft/rust/projects/matrix)
src/lib.rs:35:27:35:54错误:不匹配的类型:
应为'T`,
找到“:”输出`
(应为类型参数,
找到关联类型)[E0308]
src/lib.rs:35 res.data.push(self.data[i]+_rhs.data[i]);
^~~~~~~~~~~~~~~~~~~~~~~~~~~
根据错误报告,我想我需要在其他地方指出T实现了addtrait,但是在我尝试这样做的任何地方,我要么得到相同的错误,要么得到一个解析错误
顺便说一下,我对矩阵的定义是
pub struct Matrix<T> {
pub rows: usize,
pub cols: usize,
pub data: Vec<T>,
}
pub结构矩阵{
酒吧行:usize,
酒吧:使用,
发布数据:Vec,
}
使用T:Add
作为边界表示可以编写T+T
,但它不会对由此产生的类型设置任何限制,特别是,它可能不是T
。您依赖于它是T
才能返回矩阵
一种方法是要求T:Add
,以便T+T
返回T
:
impl<T: Add<Output = T>> Add for Matrix<T> {
...
}
但是,这两种方法都遇到了一个问题:
<anon>:23:27: 23:39 error: cannot move out of indexed content
<anon>:23 res.data.push(self.data[i] + _rhs.data[i]);
^~~~~~~~~~~~
<anon>:23:42: 23:54 error: cannot move out of indexed content
<anon>:23 res.data.push(self.data[i] + _rhs.data[i]);
^~~~~~~~~~~~
a
和b
变量都属于T
类型,即所有权已转移
请注意,实际上可以对代码进行更多的“迭代”,编写:
fn add(self, _rhs: Matrix<T>) -> Matrix<T::Output>
{
assert!(self.rows == _rhs.rows && self.cols == _rhs.cols,
"attempting to add matrices of different sizes");
let data = self.data.into_iter()
.zip(_rhs.data.into_iter())
.map(|(a,b)| a + b)
.collect();
Matrix {
rows: self.rows,
cols: self.cols,
data: data
}
}
fn添加(自身,矩阵)->矩阵
{
断言!(self.rows==\u rhs.rows&&self.cols==\u rhs.cols,
“试图添加不同大小的矩阵”);
让data=self.data.into_iter()
.zip(_rhs.data.into_iter())
.map(|(a,b)| a+b)
.收集();
母体{
行:self.rows,
科尔斯:赛尔夫·科尔斯,
数据:数据
}
}
使用T:Add
作为边界表示可以编写T+T
,但它不会对由此产生的类型设置任何限制,特别是,它可能不是T
。您依赖于它是T
才能返回矩阵
一种方法是要求T:Add
,以便T+T
返回T
:
impl<T: Add<Output = T>> Add for Matrix<T> {
...
}
但是,这两种方法都遇到了一个问题:
<anon>:23:27: 23:39 error: cannot move out of indexed content
<anon>:23 res.data.push(self.data[i] + _rhs.data[i]);
^~~~~~~~~~~~
<anon>:23:42: 23:54 error: cannot move out of indexed content
<anon>:23 res.data.push(self.data[i] + _rhs.data[i]);
^~~~~~~~~~~~
a
和b
变量都属于T
类型,即所有权已转移
请注意,实际上可以对代码进行更多的“迭代”,编写:
fn add(self, _rhs: Matrix<T>) -> Matrix<T::Output>
{
assert!(self.rows == _rhs.rows && self.cols == _rhs.cols,
"attempting to add matrices of different sizes");
let data = self.data.into_iter()
.zip(_rhs.data.into_iter())
.map(|(a,b)| a + b)
.collect();
Matrix {
rows: self.rows,
cols: self.cols,
data: data
}
}
fn添加(自身,矩阵)->矩阵
{
断言!(self.rows==\u rhs.rows&&self.cols==\u rhs.cols,
“试图添加不同大小的矩阵”);
让data=self.data.into_iter()
.zip(_rhs.data.into_iter())
.map(|(a,b)| a+b)
.收集();
母体{
行:self.rows,
科尔斯:赛尔夫·科尔斯,
数据:数据
}
}
谢谢。我理解需要指出输出类型也是T,但我显然还没有完全了解所有权。需要更多的学习…谢谢。我理解需要指出输出类型也是T,但我显然还没有完全了解所有权。需要更多的学习。。。