generic-array (0.14.7)
Published 2026-03-26 10:32:44 +00:00 by siennathesane
Installation
[registry]
default = "gitea"
[registries.gitea]
index = "sparse+ " # Sparse index
# index = " " # Git
[net]
git-fetch-with-cli = truecargo add generic-array@0.14.7About this package
Generic types implementing functionality of arrays
generic-array
This crate implements generic array types for Rust.
Requires minumum Rust version of 1.36.0, or 1.41.0 for From<[T; N]> implementations
Usage
The Rust arrays [T; N] are problematic in that they can't be used generically with respect to N, so for example this won't work:
struct Foo<N> {
data: [i32; N]
}
generic-array defines a new trait ArrayLength<T> and a struct GenericArray<T, N: ArrayLength<T>>, which let the above be implemented as:
struct Foo<N: ArrayLength<i32>> {
data: GenericArray<i32, N>
}
The ArrayLength<T> trait is implemented by default for unsigned integer types from typenum crate:
use generic_array::typenum::U5;
struct Foo<N: ArrayLength<i32>> {
data: GenericArray<i32, N>
}
fn main() {
let foo = Foo::<U5>{data: GenericArray::default()};
}
For example, GenericArray<T, U5> would work almost like [T; 5]:
use generic_array::typenum::U5;
struct Foo<T, N: ArrayLength<T>> {
data: GenericArray<T, N>
}
fn main() {
let foo = Foo::<i32, U5>{data: GenericArray::default()};
}
In version 0.1.1 an arr! macro was introduced, allowing for creation of arrays as shown below:
let array = arr![u32; 1, 2, 3];
assert_eq!(array[2], 3);
Dependencies
| ID | Version |
|---|---|
| serde | ^1.0 |
| typenum | ^1.12 |
| zeroize | ^1 |
| bincode | ^1.0 |
| serde_json | ^1.0 |
| version_check | ^0.9 |
Keywords
generic
array
Details
2026-03-26 10:32:44 +00:00
Assets (1)
Versions (1)
View all
Cargo
0
Bartłomiej Kamiński <fizyk20@gmail.com>
Aaron Trent <novacrazy@gmail.com>
MIT
17 KiB
generic-array-0.14.7.crate
17 KiB
0.14.7
2026-03-26