I have a tag trait that has a associated constant:
trait Trait {
const N: usize;
}
I have a type that is generic, bounded by the trait. I'd like to be able to use N in the declaration:
struct S<T: Trait> {
field: [f64; T::N],
}
My expectation is that for any implementation of Trait that specifies N, I can have an S with an array of that many floats in it. Instead, I get the following compiler error:
error: generic parameters may not be used in const operations
--> src/main.rs:8:18
|
8 | field: [f64; T::N],
| ^^^^ cannot perform const operation using `T`
|
= note: type parameters may not be used in const expressions
error[E0392]: parameter `T` is never used
--> src/main.rs:7:10
|
7 | struct S<T: Trait> {
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
For more information about this error, try `rustc --explain E0392`.
While I understand the error itself, I don't understand the reason for it or how to get around it.
The
traitneeds animplto define thatconst N.Try this: