3 releases
0.1.2 | Dec 28, 2024 |
---|---|
0.1.1 | Dec 26, 2024 |
0.1.0 | Dec 26, 2024 |
#1262 in Data structures
328 downloads per month
32KB
671 lines
arrayy
Stack-allocated fixed-size array with useful methods on top of Rust's [T; L]
type.
Methods
new
len
set_len
set_len_unchecked
buf
buf_mut
capacity
is_empty
get
get_mut
get_unchecked
get_unchecked_mut
last
last_mut
first
first_mut
from
push
push_unchecked
pop
pop_unchecked
iter
iter_mut
into_iter
append
append_slice
append_unchecked
append_slice_unchecked
filter
map
truncate
as_slice
as_mut_slice
as_vec
insert
insert_unchecked
remove
remove_unchecked
clear
as_mut_ptr
as_ptr
as_mut_ptr_range
as_ptr_range
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]);