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

Debug-format the slice of filled elements (potentially less than N).

Initialize an empty PartialArray.

A PartialArray<T, _> dereferences to a slice of T.

Dereference to the slice of filled elements (potentially less than N).

Dereference to the slice of filled elements (potentially less than N).

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

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).

Feeds a slice of this type into the given Hasher. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Compare two PartialArrays 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

Restrict a value to a certain interval. Read more

Compare the slice of 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);

This method tests for !=.

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);

This method tests for !=.

Compare the filled elements of PartialArrays.

Two PartialArrays 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);

This method tests for !=.

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);

This method tests for !=.

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);

This method tests for !=.

Compare two PartialArrays 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.