I am having trouble using Option within a struct and being able to access members of the struct in init() function. Rust newbie - I would appreciate some advice on how to get around this.
extern crate ndarray;
use ndarray::prelude::*;
struct Block {
voxels: Option<Array3::<u8>>,
}
impl Block {
fn init(&mut self, size: usize) {
self.voxels = Some(Array3::<u8>::from_elem((size, size, size), 0));
// Set a value in the voxel array
match &self.voxels {
Some(mut voxels) => {
voxels[[1, 1, 1]] = 1;
},
None => (),
}
}
}
fn main() {
let mut block = Block {
voxels: None,
};
block.init(10);
match block.voxels {
Some(voxels) => {
println!("r = {:?}", voxels[[1,1,1]]);
},
None => {
println!("nada");
}
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of a shared reference
--> src/main.rs:16:15
|
16 | match &self.voxels {
| ^^^^^^^^^^^^
17 | Some(mut voxels) => {
| ----------
| |
| data moved here
| move occurs because `voxels` has type `ArrayBase<OwnedRepr<u8>, Dim<[usize; 3]>>`, which does not implement the `Copy` trait
For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error