I'm having trouble initializing a fixed length array. My attempts so far all result in the same "use of possibly uninitialized variable: foo_array
" error:
#[derive(Debug)]
struct Foo { a: u32, b: u32 }
impl Default for Foo {
fn default() -> Foo { Foo{a:1, b:2} }
}
pub fn main() {
let mut foo_array: [Foo; 10];
// Do something here to in-place initialize foo_array?
for f in foo_array.iter() {
println!("{:?}", f);
}
}
error[E0381]: use of possibly uninitialized variable: `foo_array`
--> src/main.rs:13:14
|
13 | for f in foo_array.iter() {
| ^^^^^^^^^ use of possibly uninitialized `foo_array`
I implemented the Default
trait, but Rust does not seem to call this by default akin to a C++ constructor.
What is the proper way to initialize a fixed length array? I'd like to do an efficient in-place initialization rather than some sort of copy.
Related: Why is the Copy trait needed for default (struct valued) array initialization?
Related: Is there a way to not have to initialize arrays twice?
The safe but somewhat inefficient solution:
#[derive(Copy, Clone, Debug)]
struct Foo { a: u32, b: u32 }
fn main() {
let mut foo_array = [Foo { a: 10, b: 10 }; 10];
}
Since you're specifically asking for a solution without copies:
use std::mem;
use std::ptr;
#[derive(Debug)]
struct Foo { a: u32, b: u32 }
// We're just implementing Drop to prove there are no unnecessary copies.
impl Drop for Foo {
fn drop(&mut self) {
println!("Destructor running for a Foo");
}
}
pub fn main() {
let array = unsafe {
// Create an uninitialized array.
let mut array: [Foo; 10] = mem::uninitialized();
for (i, element) in array.iter_mut().enumerate() {
let foo = Foo { a: i as u32, b: 0 };
// Overwrite `element` without running the destructor of the old value.
// Since Foo does not implement Copy, it is moved.
ptr::write(element, foo)
}
array
};
for element in array.iter() {
println!("{:?}", element);
}
}
You can use the arrayvec
crate:
Cargo.toml
[package]
name = "initialize_array"
version = "0.1.0"
authors = ["author"]
[dependencies]
arrayvec = "0.3.20"
src/main.rs
extern crate arrayvec;
use arrayvec::ArrayVec;
use std::iter;
#[derive(Clone)]
struct Foo {
a: u32,
b: u32,
}
fn main() {
let foo_array: [Foo; 10] = iter::repeat(Foo { a: 10, b: 10})
.collect::<ArrayVec<_>>()
.into_inner()
.unwrap_or_else(|_| unreachable!());
}