Rust编译器在这里说了什么?

2guxujil  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(168)
error[E0621]: explicit lifetime required in the type of `is`
  --> src/bee.rs:76:45
   |
75 | fn compute_instruction_set<'a>(is: &'a mut InstructionSet) {
   |                                    ---------------------- help: add explicit lifetime `'a` to the type of `is`: `&'a mut [OpCode<'a>; 255]`
76 |     let mut isb = InstructionSetBuilder{is: is, pos: 0};
   |                                             ^^ lifetime `'a` required

这是代码:

type InstructionSet<'a> = [OpCode<'a>; 0xff];
...
struct InstructionSetBuilder<'a> {
    is: &'a mut [OpCode<'a>],
    pos: usize,
}
...
fn compute_instruction_set<'a>(is: &'a mut InstructionSet) {
    let mut isb = InstructionSetBuilder{is: is, pos: 0};
...
}

为什么我需要设置另一个“一生”参数?
在哪里?
我试了几种组合,都破坏了语法......

uttx8gqw

uttx8gqw1#

type InstructionSet<'a> = [OpCode<'a>; 0xff];

此类型别名在生存期'a内是泛型的。因此必须指定它。请尝试使用此别名。

fn compute_instruction_set<'a>(is: &'a mut InstructionSet<'a>) {
    let mut isb = InstructionSetBuilder{is: is, pos: 0};
...
}

相关问题