¿Cómo inicializar por defecto una estructura que contiene una matriz en Rust?
¿Cuál es la forma recomendada de declarar una estructura que contiene una matriz y luego crear una instancia con inicialización cero?
Aquí está la estructura:
#[derive(Default)]
struct Histogram {
sum: u32,
bins: [u32; 256],
}
y el error del compilador:
error[E0277]: the trait bound `[u32; 256]: std::default::Default` is not satisfied
--> src/lib.rs:4:5
|
4 | bins: [u32; 256],
| ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u32; 256]`
|
= help: the following implementations were found:
<[T; 14] as std::default::Default>
<&'a [T] as std::default::Default>
<[T; 22] as std::default::Default>
<[T; 7] as std::default::Default>
and 31 others
= note: required by `std::default::Default::default`
Si intento agregar el inicializador faltante para la matriz:
impl Default for [u32; 256] {
fn default() -> [u32; 255] {
[0; 256]
}
}
Yo obtengo:
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> src/lib.rs:7:5
|
7 | impl Default for [u32; 256] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate
|
= note: the impl does not reference any types defined in this crate
= note: define and implement a trait or new type instead
¿Estoy haciendo algo mal?