Skip to content

Instantly share code, notes, and snippets.

@lu-zero
Forked from anonymous/playground.rs
Created August 1, 2017 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lu-zero/14f6c05df46a3374dabeb946444214e9 to your computer and use it in GitHub Desktop.
Save lu-zero/14f6c05df46a3374dabeb946444214e9 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
/* 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