#stack-allocated #array-vec #heap-allocated #vec-t #size #moves

vec_to_array

Moves a heap allocated Vec<T> to an stack allocated array of type T and size N

7 releases

0.2.5 Sep 7, 2024
0.2.4 Sep 7, 2024
0.2.3 Oct 26, 2023
0.1.0 Oct 26, 2023

#810 in Algorithms

Download history 9/week @ 2024-10-24 2/week @ 2024-10-31 2/week @ 2024-11-07 2/week @ 2024-11-14 30/week @ 2024-11-21 15/week @ 2024-11-28 88/week @ 2024-12-05 59/week @ 2024-12-12 8/week @ 2024-12-19 8/week @ 2025-01-09 12/week @ 2025-01-16 10/week @ 2025-01-23 1/week @ 2025-01-30 31/week @ 2025-02-06

62 downloads per month

MIT license

8KB
108 lines

vec_to_array

Moves a heap allocated Vec into a stack allocated array.

let vec: Vec<i64> = vec![1, 2, 3];
let array: [i64; 3] = vec_to_array!(vec, i64, 3);
assert_eq!(array, [1, 2, 3]);

let vec: Vec<i32> = vec![1, 2, 3];
let array: Result<[i32; 3], VecToArrayError> = try_vec_to_array!(vec, i32, 3);
assert_eq!(array.unwrap(), [1, 2, 3]);

Note, 1.48.0 introduced an implementation of try_into for transmuting directly on the heap, which should usually be preferred.


lib.rs:

Moves a heap allocated Vec<T> to an stack allocated array of type T and size N.

No runtime deps