Expand description
This crate offers the ArrayTools
extension trait, which provides
a variety of helpful methods for working with fixed-size arrays.
§Examples
Iterator
-like methods over arrays:
use arraytools::ArrayTools;
assert_eq!([1, 2, 3].map(|x| x+1), [2, 3, 4]);
assert_eq!([1, 2].zip(["one", "two"]), [(1, "one"), (2, "two")]);
Ways to simplify array creation:
use arraytools::ArrayTools;
let mut state = 1;
assert_eq!(<[_; 4]>::generate(|| { state *= 2; state }), [2, 4, 8, 16]);
assert_eq!(<[usize; 4]>::indices(), [0, 1, 2, 3]);
let s = "hello".to_string(); // Something `!Copy`
assert_eq!(<[String; 3]>::repeat(s).as_ref_array(), ["hello", "hello", "hello"]);
Conversion to and from homogeneous tuples:
use arraytools::ArrayTools;
let mut array = [2, 3, 5, 7, 11];
assert_eq!(array.into_tuple(), (2, 3, 5, 7, 11));
array = ArrayTools::from_tuple((1, 1, 2, 3, 5));
assert_eq!(array, [1, 1, 2, 3, 5]);
Like Option
, most combinators here take self
. To not move something,
you can use .as_ref_array()
or .as_mut_array()
:
use arraytools::ArrayTools;
struct SevenStrings([String; 7]);
impl SevenStrings {
fn push_str(&mut self, s: &str) {
self.0.as_mut_array().for_each(|x: &mut String| x.push_str(s));
}
}
Traits§
- Array
Tools - An extension trait for working with fixed-length arrays.