Created
January 18, 2021 16:26
-
-
Save YoshiTheChinchilla/3c1356add70055f8f0b706d0252413e0 to your computer and use it in GitHub Desktop.
Zero array with generics
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
#![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