我正在通过实现矩阵数学来练习 Rust,但遇到了一些障碍。我定义了我认为与矩阵相关的特征。
trait Matrix<T> where T : num::Num {
fn dim(&self) -> (usize, usize);
fn elem(&self, i : usize, j : usize) -> Option<& T>;
fn new_const(v: T, rows : usize, cols : usize) -> Self where T : Clone;
fn same_dim<U>(&self, other: &U) -> bool where U : Matrix<T> {
self.dim() == other.dim()
}
}
我有一个使用 Vec<Vec<T>>
的愚蠢实现.我实现了所有的方法并测试了它们。他们都在工作。现在我想简单地将两个矩阵相加。因此,在不添加我知道需要的行迭代器并执行我知道不正确的添加实现的情况下,我输入了以下内容。
impl <T, U> Add for U where T: num::Num, U: Matrix<T> {
type Output = U;
fn add(self, _rhs: U) -> U {
U::new_const(T::zero(), 5, 5)
}
}
但是我明白了
lib.rs:41:7: 41:8 error: the type parameter `T` is not constrained by the impl trait, self type, or predicates [E0207]
lib.rs:41 impl <T, U> Add for U where T: num::Num, U: Matrix<T> {
^
lib.rs:41:7: 41:8 help: run `rustc --explain E0207` to see a detailed explanation
error: aborting due to previous error
Could not compile `matrix`.
To learn more, run the command again with --verbose.
Compilation failed.
在我看来,T 确实受到了限制。谁能指出我正确的方向?
最佳答案
T
不受编译器可以在实现的使用站点看到的信息的限制。通过拥有 trait Matrix<T>
,单个类型可以是多个元素类型的矩阵,也就是说,某人同时拥有 impl Matrix<u8> for Foo
是完全合法的和 impl Matrix<u16> for Foo
在同一个程序中。如果发生这种情况,则 Foo + Foo
(即使用 Add
实现)不能确定哪个 T
使用:两者T = u8
和 T = u16
工作。
我认为解决此问题的最佳方法是删除 T
类型参数:给定类型只是一种元素的矩阵(即使它是通用的),例如Vec<Vec<T>>
是 T
上的矩阵, 没有其他的。这就像 Iterator
: 给定的类型只能产生一种类型的元素。
在代码中,这看起来像:
trait Matrix {
type Elem: num::Num;
fn dim(&self) -> (usize, usize);
fn elem(&self, i: usize, j: usize) -> Option<&Self::Elem>;
// ...
}
Add
然后实现变成
impl<U> Add for U where U: Matrix {
type Output = U;
fn add(self, _rhs: U) -> U {
U::new_const(U::Elem::zero(), 5, 5)
}
}
但是,这也行不通,因为它会遇到连贯性问题。编译器不能确定 Add
的这个实现不与 Add
的其他实现重叠,因为可以实现 Matrix
对于已经有 Add
的类型以某种方式实现。可以通过制作包装器类型并使用 Matrix
来解决这个问题trait 而不是作为“后备存储”trait,即它“只是”控制内部表示。
struct Matrix<T: Storage> {
x: T
}
trait Storage { // renamed `Matrix` trait
type Elem;
fn dim(&self) -> (usize, usize);
// ...
}
Matrix
type 然后成为添加更多方法和特征实现等的地方。
关于generics - 用泛型全面实现特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809355/