Crate vectrs[][src]

NOTE: this crate was renamed to `vectrix`, please use it under the new name.

See vectrix

This crate provides a stack-allocated, constant-size, n-dimensional Vector<T, N> type.

Constructors

vector! macro

Simply use the vector! macro to construct a new Vector of any size.

let v = vector![1, 3, 3, 7];
//  ^ constructs a `Vector<_, 4>`.

Directly using new() or From

A Vector is backed by an array. The simplest way to construct a Vector is to create it directly from an array or tuple. In both of these cases the size of the Vector can be easily inferred by the Rust type system.

From an array:

let v = Vector::new([1, 2, 3, 4]);
//  ^ Rust automatically infers that the type is `Vector<_, 4>`.

From a tuple:

// ... 1 to 12 element tuples are supported
let v = Vector::from((1, 2, 3));
//  ^ Rust automatically infers that the type is `Vector<_, 3>`.

Collecting from an iterator

The other common method of constructing a Vector is to use the .collect() method on an iterator. When collecting from an iterator, .collect() will panic if there are not enough elements to fill the Vector. If there are extra elements they will be ignored.

let heap = vec![1, 2, 3, 4, 5];
let stack: Vector<_, 5> = heap.into_iter().collect();
//         ^^^^^^^^^^^^ the type needs to be provided in this case

Using from_partial{_with}

It is common that you do not have enough elements to fill the Vector. So the .from_partial() and .from_partial_with() methods are provided. These can be used to construct a Vector and fill the remaining space with zeroes or a fill value.

let v = Vector::<_, 3>::from_partial((1, 2));
assert_eq!(v, Vector::new([1, 2, 0]));

let v = Vector::<_, 5>::from_partial_with((3, 2, 1), 1);
assert_eq!(v, Vector::new([3, 2, 1, 1, 1]));     // ^ fill value

Accessing and mutating data

Slice representation

A slice view of the underlying data is provided using Deref or .as_slice(). This means all slice methods are available including indexing.

let vector = vector![1, 3, 3, 7];
assert_eq!(vector[3], 7);

A mutable slice view of the underlying data is provide using DerefMut or .as_mut_slice(). This means you can mutate data using slice indexing.

let mut vector = vector![1, 3, 3, 7];;
vector[0] = 2;
assert_eq!(vector, vector![2, 3, 3, 7]);

Component accessor methods

Component accessor methods are available for small vectors using commonly recognized names.

let vector = vector![1, 3, 3, 7];
assert_eq!(vector.x(), 1);
assert_eq!(vector.y(), 3);
assert_eq!(vector.z(), 3);
assert_eq!(vector.w(), 7);

Additionally, you can get mutable access using the *_mut versions.

let mut vector = vector![1, 3, 3, 7];
*vector.y_mut() = 2;
*vector.w_mut() = 4;
assert_eq!(vector, vector![1, 2, 3, 4]);

Modules

traits

Abstractions over number types.

Macros

vector

Construct a new Vector of any size.

Structs

IntoIter

An iterator that moves out of a vector.

Vector

Represents a constant-size, n-dimensional vector.