Vector

Struct Vector 

Source
pub struct Vector<T, const N: usize>(/* private fields */);
Expand description

A generic, fixed-size vector with compile-time dimension checking.

The Vector type represents a mathematical vector with N components of type T. It provides common vector operations and integrates with the vector-space trait ecosystem.

§Type Parameters

  • T: The scalar type of vector components (must implement relevant traits for operations)
  • N: The dimension of the vector (must be a compile-time constant)

§Examples

Basic usage with f32:

use simple_vectors::Vector;

let v = Vector::new([1.0, 2.0, 3.0]);
assert_eq!(v[0], 1.0);
assert_eq!(v[1], 2.0);
assert_eq!(v[2], 3.0);

Using with integer types:

use simple_vectors::Vector;

let int_vec = Vector::new([1, 2, 3, 4]);
let scaled = int_vec * 2;
assert_eq!(scaled, Vector::new([2, 4, 6, 8]));

Implementations§

Source§

impl<T, const N: usize> Vector<T, N>

Source

pub const fn new(elements: [T; N]) -> Self

Creates a new vector from the given array of elements.

This is a const function, allowing vector creation in const contexts.

§Arguments
  • elements - An array of N elements to initialize the vector
§Examples
use simple_vectors::Vector;

const V: Vector<i32, 3> = Vector::new([1, 2, 3]);
assert_eq!(V, Vector::new([1, 2, 3]));

Creating a 2D vector:

use simple_vectors::Vector;

let point = Vector::new([10.5, 20.3]);

Trait Implementations§

Source§

impl<T, const N: usize> Add for Vector<T, N>
where T: Add<Output = T> + Copy,

Source§

type Output = Vector<T, N>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T, const N: usize> AddAssign for Vector<T, N>
where T: AddAssign + Copy,

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<T: Real, const N: usize, const I: usize> Basis<I> for Vector<T, N>

Source§

fn unit_basis() -> Self

Creates an unit basis of the vector space.
Source§

fn basis_of(magnitude: T) -> Self

Creates a scaled basis of the vector space of the specified magnitude.
Source§

fn basis(&self) -> Self::Scalar

Queries a basis of a vector space.
Source§

fn basis_mut(&mut self) -> &mut Self::Scalar

Queries a basis of a vector space as a mutable reference.
Source§

fn with_basis(self, magnitude: Self::Scalar) -> Self

Creates a new vector with a single basis set to a different value.
Source§

impl<T: Clone, const N: usize> Clone for Vector<T, N>

Source§

fn clone(&self) -> Vector<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for Vector<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + Copy, const N: usize> Default for Vector<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const N: usize> Div<T> for Vector<T, N>
where T: Div<Output = T> + Copy,

Source§

type Output = Vector<T, N>

The resulting type after applying the / operator.
Source§

fn div(self, other: T) -> Self

Performs the / operation. Read more
Source§

impl<T, const N: usize> DivAssign<T> for Vector<T, N>
where T: DivAssign + Copy,

Source§

fn div_assign(&mut self, other: T)

Performs the /= operation. Read more
Source§

impl<T: Real, const N: usize> DotProduct for Vector<T, N>

Source§

type Output = <Vector<T, N> as VectorSpace>::Scalar

The output type of the dot product.
Source§

fn dot(self, other: Self) -> T

The dot product.
Source§

impl<'a, T, const N: usize> From<&'a Vector<T, N>> for &'a [T]

Source§

fn from(val: &'a Vector<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T, const N: usize> From<&'a Vector<T, N>> for &'a [T; N]

Source§

fn from(val: &'a Vector<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T, const N: usize> From<&'a mut Vector<T, N>> for &'a mut [T]

Source§

fn from(val: &'a mut Vector<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T, const N: usize> From<&'a mut Vector<T, N>> for &'a mut [T; N]

Source§

fn from(val: &'a mut Vector<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: usize> From<[T; N]> for Vector<T, N>

Source§

fn from(elements: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: usize> From<Vector<T, N>> for [T; N]

Source§

fn from(val: Vector<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash, const N: usize> Hash for Vector<T, N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<I, T, const N: usize> Index<I> for Vector<T, N>
where [T; N]: Index<I>,

Source§

type Output = <[T; N] as Index<I>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<I, T, const N: usize> IndexMut<I> for Vector<T, N>
where [T; N]: IndexMut<I>,

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: Real, const N: usize> InnerSpace for Vector<T, N>

Source§

fn scalar(self, other: Self) -> T

The scalar product.
Source§

fn magnitude2(self) -> Self::Scalar

The squared magnitude. Read more
Source§

fn magnitude(self) -> Self::Scalar

The magnitude of a vector.
Source§

fn normalize(self) -> Self

The normalized vector.
Source§

fn angle(self, other: Self) -> Self::Scalar

The angle between two vectors.
Source§

fn with_magnitude(self, magnitude: Self::Scalar) -> Self

Sets the magnitude of a vector.
Source§

fn with_direction(self, dir: Self) -> Self

Sets the direction of a vector.
Source§

fn query_axis(self, dir: Self) -> Self::Scalar

The value of the vector along the specified axis.
Source§

fn normalized_project(self, dir: Self) -> Self

Projects a vector onto an already normalized direction vector.
Source§

fn project(self, dir: Self) -> Self

Projects a vector onto an arbitraty direction vector.
Source§

fn normalized_reject(self, dir: Self) -> Self

Rejects a vector from an already normalized direction vector.
Source§

fn reject(self, dir: Self) -> Self

Rejects a vector from an arbitraty direction vector.
Source§

fn normalized_reflect(self, dir: Self) -> Self

Reflects a vector from an already normalized direction vector.
Source§

fn reflect(self, dir: Self) -> Self

Reflects a vector from an arbitraty direction vector.
Source§

impl<T, const N: usize> Mul<T> for Vector<T, N>
where T: Mul<Output = T> + Copy,

Source§

type Output = Vector<T, N>

The resulting type after applying the * operator.
Source§

fn mul(self, other: T) -> Self

Performs the * operation. Read more
Source§

impl<T, const N: usize> Mul for Vector<T, N>
where T: Add<Output = T> + Mul<Output = T> + Zero + Copy,

Source§

type Output = T

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> T

Performs the * operation. Read more
Source§

impl<T, const N: usize> MulAssign<T> for Vector<T, N>
where T: MulAssign + Copy,

Source§

fn mul_assign(&mut self, other: T)

Performs the *= operation. Read more
Source§

impl<T, const N: usize> Neg for Vector<T, N>
where T: Neg<Output = T> + Copy,

Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<T: PartialEq, const N: usize> PartialEq for Vector<T, N>

Source§

fn eq(&self, other: &Vector<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: usize> Sub for Vector<T, N>
where T: Sub<Output = T> + Copy,

Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<T, const N: usize> SubAssign for Vector<T, N>
where T: SubAssign + Copy,

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<T: Real, const N: usize> VectorSpace for Vector<T, N>

Source§

type Scalar = T

The scalar type of the vector space.
Source§

impl<T: Zero + Copy, const N: usize> Zero for Vector<T, N>

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<T: Copy, const N: usize> Copy for Vector<T, N>

Source§

impl<T: Eq, const N: usize> Eq for Vector<T, N>

Source§

impl<T, const N: usize> StructuralPartialEq for Vector<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Vector<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Vector<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Vector<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Vector<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for Vector<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Vector<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Bases for T
where T: VectorSpace,

Source§

fn unit_bases<const I: usize>() -> Self
where Self: Basis<I>,

Creates the specified unit basis of the vector space.
Source§

fn bases_of<const I: usize>(magnitude: Self::Scalar) -> Self
where Self: Basis<I>,

Creates the specified basis of the vector of the specified magnitude.
Source§

fn bases<const I: usize>(&self) -> Self::Scalar
where Self: Basis<I>,

Queries the specified basis of a vector space.
Source§

fn bases_mut<const I: usize>(&mut self) -> &mut Self::Scalar
where Self: Basis<I>,

Queries the specified basis of a vector space as a mutable reference.
Source§

fn with_bases<const I: usize>(self, magnitude: Self::Scalar) -> Self
where Self: Basis<I>,

Creates a new vector with the specified basis set to a different value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.