You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before Rust 1.51, arrays [T; N] were problematic in that they couldn't be generic with respect to the length N, so this wouldn't work:
structFoo<N>{data:[i32;N],}
Since 1.51, the below syntax is valid:
structFoo<constN:usize>{data:[i32;N],}
However, the const-generics we have as of writing this are still the minimum-viable product (min_const_generics), so many situations still result in errors, such as this example:
traitBar{constLEN:usize;// Error: cannot perform const operation using `Self`fnbar(&self) -> Foo<{Self::LEN}>;}
generic-array defines a new trait ArrayLength and a struct GenericArray<T, N: ArrayLength>, which lets the above be implemented as:
The ArrayLength trait is implemented for unsigned integer types from typenum crate. For example, GenericArray<T, U5> would work almost like [T; 5]:
use generic_array::typenum::U5;structFoo<T,N:ArrayLength>{data:GenericArray<T,N>}let foo = Foo::<i32,U5>{data:GenericArray::default()};
The arr! macro is provided to allow easier creation of literal arrays, as shown below:
let array = arr![1,2,3];// array: GenericArray<i32, typenum::U3>assert_eq!(array[2],3);
Feature flags
[dependencies.generic-array]
features = [
"serde", # Serialize/Deserialize implementation"zeroize", # Zeroize implementation for setting array elements to zero"const-default", # Compile-time const default value support via trait"alloc", # Enables From/TryFrom implementations between GenericArray and Vec<T>/Box<[T]>"faster-hex"# Enables internal use of the `faster-hex` crate for faster hex encoding via SIMD
]