rust - 无法将切片索引转换为 u8 类型以插入到宏期望表达式中
问题描述
我刚开始编码Rust
3 天,所以请耐心等待。我声明了一个宏,需要使用 2 个整数来索引向量并返回值。这 2 个整数将在 main 函数中从 2 个向量中提供,它们的值将插入宏中。我知道向量元素的类型是usize
需要强制转换的,u8
但这也不起作用。这是代码。
macro_rules! dummyMacro {
($x:expr, $y:expr) => {
{
let table = vec![1,2,3,4,5,6,7];
table[$x + $y]
}
};
}
fn main() {
let mut vectorOne: Vec<u8> = Vec::new();
vectorOne.push(1);
vectorOne.push(2);
let mut vectorTwo: Vec<u8> = Vec::new();
vectorTwo.push(1);
vectorTwo.push(2);
let mut vectorThree: Vec<u8> = vec![0,0];
vectorThree[0] = dummyMacro!(vectorOne[0], vectorTwo[0]);
println!("{:?}", vectorThree);
}
错误
error[E0277]: the type `[{integer}]` cannot be indexed by `u8`
--> src/main.rs:10:13
|
10 | table[$x + $y]
| ^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
...
49 | vectorThree[0] = dummyMacro!(vectorOne[0], vectorTwo[0]);
| --------------------------------------- in this macro invocation
|
= help: the trait `std::slice::SliceIndex<[{integer}]>` is not implemented for `u8`
= note: required because of the requirements on the impl of `std::ops::Index<u8>` for `std::vec::Vec<{integer}>`
我看过以下问题,但没有帮助
解决方案
推荐阅读
- angular - 如何为部署设置角度类参考路径?
- java - Guava RateLimiter 在延迟后不应用节流
- mysql - 从另一个 MYSQL 表中更新列,不包括现有数据
- prolog - 函数接受一个元素和一个列表并检查列表的长度是否等于元素
- javascript - 试图为特定场景建立一个随机变量,但结果似乎忽略了“if”语句
- python - 执行if条件嵌套在python for循环中的有效方法
- python - 使用 python-docx 访问其他段落样式属性
- python - 获取图中的 X,Y 位置(不在图中)
- c - 操作系统开发,浮点除法问题导致 NaN
- flutter - ChangeNotifier 侦听器和处置。内存泄漏?