Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Last active August 7, 2023 23:13
Show Gist options
  • Select an option

  • Save jmcph4/92916770662f36cd9a92495879f978a8 to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/92916770662f36cd9a92495879f978a8 to your computer and use it in GitHub Desktop.
use std::mem;
#[repr(C)]
pub struct Foo {
a: u128,
b: u128,
}
fn main() {
let xs: [Foo; 8] = [
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
];
println!("{}", mem::size_of::<Foo>());
println!("{}", mem::size_of::<[Foo; 8]>());
assert!(xs.len() * mem::size_of::<Foo>() == mem::size_of::<[Foo; 8]>());
}
#[repr(C)]
pub struct Foo {
a: u128,
b: u128,
}
fn main() {
let xs: [Foo; 8] = [
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
Foo { a: 1, b: 2 },
];
let ys: [u128; 8] = xs
.iter()
.map(|x| x.a * x.b)
.collect::<Vec<u128>>()
.try_into()
.unwrap();
dbg!(&ys);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment