#iterator #collect #slice #array #stack

collect_slice

Collect an iterator into a slice

3 stable releases

Uses old Rust 2015

1.2.0 Dec 4, 2016
1.1.0 Dec 2, 2016
1.0.0 Dec 24, 2015

#43 in #collect

Download history 58/week @ 2023-12-07 34/week @ 2023-12-14 34/week @ 2023-12-21 7/week @ 2023-12-28 16/week @ 2024-01-04 51/week @ 2024-01-11 68/week @ 2024-01-18 71/week @ 2024-01-25 105/week @ 2024-02-01 249/week @ 2024-02-08 145/week @ 2024-02-15 108/week @ 2024-02-22 91/week @ 2024-02-29 65/week @ 2024-03-07 91/week @ 2024-03-14 101/week @ 2024-03-21

368 downloads per month
Used in 3 crates

MIT license

11KB
130 lines

collect_slice

Documentation

Collect an iterator into a slice.

Rust comes with the Iterator::collect method for collecting an iterator's items into a heap-allocated Vec or any other type that implements FromIterator, but there's no way to collect items into a stack-allocated array without manually looping over the iterator. This crates provides an alternative with collect_slice methods that collect an iterator's items into a mutable slice (of a stack-allocated array or otherwise.)

The trait is automatically implemented for any type that implements Iterator.

Examples

use collect_slice::CollectSlice;

let mut orig = [0; 8];
(0..8).map(|i| i * 2).collect_slice_checked(&mut orig[..]);
assert_eq!(orig, [0, 2, 4, 6, 8, 10, 12, 14]);

let mut buf = [42; 8];
orig.iter()
    .map(|&x| x + 10)
    .collect_slice_checked(&mut buf[..]);
assert_eq!(buf, [10, 12, 14, 16, 18, 20, 22, 24]);

Usage

This crate can be used through cargo by adding it as a dependency in Cargo.toml:

[dependencies]
collect_slice = "^1.2.0"

and importing it in the crate root:

extern crate collect_slice;

The provided methods can then be used by importing the trait within individual modules:

use collect_slice::CollectSlice;

No runtime deps