在特征函数中引用特征const

时间:2019-12-07 10:03:46

标签: rust traits

假设我具有以下特征

pub trait MyTrait {
    const A_NUMBER: usize;
    fn run(&self, data: [u32; Self::A_NUMBER]) -> u32;
}

这会导致以下输出:

error[E0599]: no associated item named `A_NUMBER` found for type `Self` in the current scope
 --> src/opcode.rs:8:37
  |
8 |     fn run(&self, data: [u32; Self::A_NUMBER]) -> u32;
  |                                     ^^^^^^^^ associated item not found in `Self`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `A_NUMBER`, perhaps you need to restrict type parameter `Self` with it:
  |
6 | Self: opcode::MyTrait

确定此请求毫无意义吗?如果我已经为我相信MyTrait引用的结构实现了特征Self,那肯定意味着Self实现了MyTrait吗?我该如何在特征函数中正确使用特征常数,或者只是不可能?

0 个答案:

没有答案