Skip to content

Instantly share code, notes, and snippets.

@YoshiTheChinchilla
Created January 18, 2021 16:26
Show Gist options
  • Save YoshiTheChinchilla/3c1356add70055f8f0b706d0252413e0 to your computer and use it in GitHub Desktop.
Save YoshiTheChinchilla/3c1356add70055f8f0b706d0252413e0 to your computer and use it in GitHub Desktop.
Zero array with generics
#![feature(const_generics)]
#![feature(const_evaluatable_checked)]
use std::mem::size_of;
pub fn zero_array_of<T>() -> [u8; size_of::<T>()] {
[0u8; { size_of::<T>() }]
}
#[cfg(test)]
mod tests {
#[test]
fn test_zero_array_of() {
let arr = zero_array_of::<usize>();
let expected = [0, 0, 0, 0, 0, 0, 0, 0];
assert_eq!(expected, arr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment