Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Using associated constants in traits - best practice
I want to define the following trait:
pub trait MyTrait {
type T;
const N: usize;
fn compress(&self, input: [Self::T; Self::N]) -> Self::T;
}
But it doesn't work:
error: generic parameters may not be used in const operations
--> src/main.rs:5:41
|
5 | fn compress(&self, input: [Self::T; Self::N]) -> Self::T;
| ^^^^^^^ cannot perform const operation using `Self`
|
= note: type parameters may not be used in const expressions
My understanding is that associated constants in traits are simply not strong enough yet.
Now my question: What's the best practice to still make this somehow work?
const N: usize
a generic instead of an associated type. (Then it works.)Thank you!
2 posts - 2 participants
🏷️ Rust_feed