-
-
Save lu-zero/14f6c05df46a3374dabeb946444214e9 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Can you do better? | |
* | |
* The following code tries to initialize an array of somehow complex structures | |
* I added also the dumb way to do that with maps and vectors | |
* I wonder if adding some syntactic sugar such as | |
* | |
* let array = [{|index| generator(index)}; N]; | |
* | |
* Could be interesting or hadn't been proposed already. | |
* | |
* lu_zero | |
*/ | |
use std::mem; | |
#[derive(Debug)] | |
struct Codebook<T> { | |
x: T, | |
y: T | |
} | |
fn fill_thing_array(a : &mut [[Codebook<u8>; 4]; 4]) { | |
for (i, line) in a.iter_mut().enumerate() { | |
for (j, el) in line.iter_mut().enumerate() { | |
*el = Codebook { x: i as u8, y: j as u8}; | |
} | |
} | |
} | |
fn build_thing_vec() { | |
let a : Vec<Vec<Codebook<u8>>> = (0..4).map(|i| { | |
(0..4).map(|j| Codebook {x:i, y:j}).collect() | |
}).collect(); | |
println!("{:?}", a); | |
} | |
fn main() { | |
let mut a = unsafe { mem::uninitialized() }; | |
fill_thing_array(&mut a); | |
println!("{:?}", a); | |
build_thing_vec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment