Initialize a fixed length array in Rust
To initialize a fixed length array in Rust, we can use the length as second attribute of the array declaration in below example we are using length as 2. See output to find 2 times:Foo { a: 10, b: 10 }
.#[derive(Copy, Clone, Debug)]
struct Foo {
a: u32,
b: u32,
}
fn main() {
let mut foo_array = [Foo { a: 10, b: 10 }; 2];
println!("Array content = {:?} ", foo_array);
}
Output
Array content = [Foo { a: 10, b: 10 }, Foo { a: 10, b: 10 }]