Struct partial_array::PartialArray [−][src]
pub struct PartialArray<T, const N: usize> { /* fields omitted */ }
Expand description
A potentially partially filled array.
This is an array, with a length of at most N
, but any value below that is
possible. It is mainly used as a iter.collect()
target via the FromIterator
trait.
fn first_five_authors<'a>(names: &mut [&'a str]) -> PartialArray<&'a str, 5> {
names.sort();
names.iter().copied().take(5).collect() // can be less than 5 items
}
// e.g. works with 5 or more items, less than 5 or even none at all
assert_eq!(
first_five_authors(&mut ["a", "c", "b", "d", "f", "e"]),
["a", "b", "c", "d", "e"],
);
assert_eq!(
first_five_authors(&mut ["Bela Writa", "A Nauthor"]),
["A Nauthor", "Bela Writa"],
);
assert_eq!(first_five_authors(&mut []), []);
It derefs to a slice, so you can execute the usual slice operations on it.
See the crate-level-documentation for more information on the intended usage.
Trait Implementations
Clone a PartialArray
.
The whole array storage is cloned, i.e. the old and new length are the same. Uninitialized elements remain uninitialized. The number of entries in the array stays the same.
Example
let a: PartialArray::<i32, 10> = (32..37).map(|x| x * 2).collect();
let b = a.clone();
assert_eq!(a.len(), b.len());
assert_eq!(a, b);
Performs copy-assignment from source
. Read more
Initialize an empty PartialArray
.
A PartialArray<T, _>
dereferences to a slice of T
.
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Build up a PartialArray
from an iterator with potentially less than
N
elements.
Example
// a set of channels set to different values
let mut channels = [12, 13, 8, 12, 255, 8, 8, 8];
// we want to only have the distinct channel values
channels.sort_unstable();
let distinct_channels: PartialArray<_, 8> = channels
.windows(2)
.chain(Some(&[channels[7], 0][..]))
.filter(|window| window[0] != window[1])
.map(|window| window[0])
.collect();
assert_eq!(distinct_channels.len(), 4);
assert_eq!(distinct_channels, [8, 12, 13, 255]);
Panics
Panics, if the length of the iterator os greater than the maximum length
of the array (N
).
Calculate the Hash
of a PartialArray
.
This has takes only the initialized elements into account (which is in
line with the PartialEq
implementation).
Compare two PartialArray
s element-by-element.
Example
assert!(partial_array![17, 24, 25] < partial_array![17, 24, 100]);
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Compare a PartialArray
with a normal array.
This compares the filled elements (potentially less than N
).
Example
let a: PartialArray<u8, 5> = (10..15).collect();
let b = [10, 11, 12, 13, 14];
assert_eq!(a, b);
// the other way round is also possible.
assert_eq!(b, a);
impl<T: PartialEq, const N: usize, const M: usize> PartialEq<PartialArray<T, M>> for PartialArray<T, N>
impl<T: PartialEq, const N: usize, const M: usize> PartialEq<PartialArray<T, M>> for PartialArray<T, N>
Compare the filled elements of PartialArray
s.
Two PartialArray
s can be compared even if their lengths do not
match. Only the number of filled elements and their values are compared.
Example
let a: PartialArray<u8, 5> = (0..4).collect();
let b: PartialArray<u8, 500> = (0..4).collect();
assert_eq!(a, b);
Compare a normal array with a PartialArray
.
This compares the filled elements (potentially less than N
).
Example
let a = [10, 11, 12, 13, 14];
let b: PartialArray<u8, 5> = (10..15).collect();
assert_eq!(a, b);
// the other way round is also possible.
assert_eq!(b, a);
Compare a slice with a PartialArray
.
This compares the filled elements (potentially less than N
).
Example
let a: &[u8] = &[10, 11, 12, 13, 14];
let b: PartialArray<u8, 5> = (10..15).collect();
assert_eq!(a, b);
// the other way round is also possible.
assert_eq!(b, a);
Compare two PartialArray
s element-by-element.
Example
assert!(partial_array![17.0, 24.0, 2.0] < partial_array![17.0, 24.0, 9.0]);
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more