#array #methods #stack-allocated #fixed-size #top

arrayy

Stack-allocated fixed-size array with useful methods on top of Rust's [T; L] type

3 releases

0.1.2 Dec 28, 2024
0.1.1 Dec 26, 2024
0.1.0 Dec 26, 2024

#1262 in Data structures

Download history 325/week @ 2024-12-25 3/week @ 2025-01-01

328 downloads per month

MIT license

32KB
671 lines

arrayy

Stack-allocated fixed-size array with useful methods on top of Rust's [T; L] type.

Methods

Examples

Creating an Array

use arrayy::{array, Array};

let a = array![u8; 10]; // empty array (len = 0) with capacity = 10
// same as
let a: Array<u8, 10> = Array::default();

let b = array![1u8, 2, 3; 10]; // array with 3 elements (len = 3) and capacity = 10
// same as
let b = Array::<u8, 10>::from(&[1, 2, 3]);

let c = array![1u8, 2, 3]; // array with 3 elements (len = 3) and capacity = 3
// same as
let c = Array::<u8, 3>::from(&[1, 2, 3]);

Basic Operations

let mut arr = array![1, 2, 3; 10];
arr.push(4);
assert_eq!(arr.len(), 4);
assert_eq!(arr.pop(), Some(4));
assert_eq!(arr.len(), 3);

Accessing Elements

let arr = array![1, 2, 3];
assert_eq!(arr[1], 2);
assert_eq!(arr.get(1), Some(&2));
assert_eq!(arr.first(), Some(&1));
assert_eq!(arr.last(), Some(&3));

Iterating Over Elements

let arr = array![1, 2, 3];
for val in arr.iter() {
    println!("{}", val);
}

let mut arr = array![1, 2, 3];
for val in arr.iter_mut() {
    *val *= 2;
}
assert_eq!(arr, array![2, 4, 6]);

Filtering and Mapping

let arr = array![1, 2, 3, 4, 5];
let filtered = arr.filter(|&x| x % 2 == 0);
assert_eq!(filtered, array![2, 4]);

let mapped = arr.map(|&x| x * 2);
assert_eq!(mapped, array![2, 4, 6, 8, 10]);

Appending Arrays

let mut arr1 = array![1, 2, 3; 5];
let arr2 = array![4, 5];
arr1.append(&arr2);
assert_eq!(arr1, array![1, 2, 3, 4, 5]);

No runtime deps