请考虑以下特征:
trait Test {
const COUNT: usize = 12;
fn test() {
let arr: [f32; Self::COUNT]; // doesn't work
dbg!(Self::COUNT); // works
}
}
dbg!(Self::COUNT)
可以正常工作,但是我无法使用[f32; Self::COUNT]
声明数组:
error[E0599]: no associated item named `COUNT` found for type `Self` in the current scope
--> src/lib.rs:4:24
|
4 | let arr: [f32; Self::COUNT];
| ^^^^^^^^^^^ associated item not found in `Self`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `COUNT`, perhaps you need to implement it:
candidate #1: `Test`
但这在全局常量中有效:
const G_COUNT: usize = 1;
const G_ARR: [i32; G_COUNT] = [1]; // works
有没有办法使用与特质相关的常数,例如全局常数?