我试图在 Rust 中概括一些代数运算(比如群、环、域等),我在尝试实现两个“集合”(Vec 的)的叉积时遇到了这个问题。
(请注意,这是在夜间工具链中使用 const_generics
。)
fn CrossProduct<T, const N: usize, const M: usize>(lhs: Vec<[T;N]>, rhs: Vec<[T;M]>) -> Vec<[T;N+M]> {
let mut out: Vec<[T;N+M]> = Vec::with_capacity(lhs.len() * rhs.len());
for i in 0..lhs.len() {
for j in 0..rhs.len() {
let mut inner: [T; N+M] = [lhs[i][0]; N+M];
for idx in 1..N {
inner[idx] = lhs[i][idx];
}
for idx in 0..M {
inner[idx + N] = rhs[j][idx];
}
out.push(inner);
}
}
out
}
无论我在哪里使用表达式 [T;N+M]
,我都会收到一条错误消息,指出常量表达式取决于通用参数,这是没有意义的。
最佳答案
您的代码实际上是 compile on the latest nightly ,您只需要几个功能标志。
#![feature(const_generics)]
#![feature(const_evaluatable_checked)]
fn CrossProduct<T: Copy, const N: usize, const M: usize>(lhs: Vec<[T;N]>, rhs: Vec<[T;M]>) -> Vec<[T;N+M]> {
let mut out: Vec<[T;N+M]> = Vec::with_capacity(lhs.len() * rhs.len());
for i in 0..lhs.len() {
for j in 0..rhs.len() {
let mut inner: [T; N+M] = [lhs[i][0]; N+M];
for idx in 1..N {
inner[idx] = lhs[i][idx];
}
for idx in 0..M {
inner[idx + N] = rhs[j][idx];
}
out.push(inner);
}
}
out
}
fn main() {
let a = vec![[1i32,2,3],[1,2,3],[1,2,3]];
let b = vec![[1,2,3],[1,2,3],[1,2,3]];
println!("{:?}", CrossProduct(a, b));
}
当尝试仅使用 #![feature(min_const_generics)]
进行编译时,编译器实际上推荐了这个解决方案:
error: generic parameters may not be used in const operations
--> src/main.rs:2:102
|
2 | fn CrossProduct<T: Copy, const N: usize, const M: usize>(lhs: Vec<[T;N]>, rhs: Vec<[T;M]>) -> Vec<[T;N+M]> {
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments, i.e. `N`
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
关于generics - Rust 常量表达式依赖于泛型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461672/