Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Created September 18, 2022 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/294247b02a53e365e62a072536617a89 to your computer and use it in GitHub Desktop.
Save matthewjberger/294247b02a53e365e62a072536617a89 to your computer and use it in GitHub Desktop.
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7fe5dd26135d20297441093328c545c5
use std::ops::Add;
pub struct Vector<T, const LEN: usize>
where
[T; LEN]:,
{
elements: [T; LEN],
}
impl<T: Add<Output = T> + Copy, const LEN: usize> Add for Vector<T, { LEN }> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
let mut elements: [T; LEN] = self.elements;
elements
.iter_mut()
.zip(rhs.elements.iter())
.for_each(|(a, b)| *a = *a + *b);
Self { elements }
}
}
pub type Vector3 = Vector<f32, 3>;
fn main() {
let a = Vector3 {
elements: [0.0, 1.0, 2.0],
};
let b = Vector3 {
elements: [0.0, 1.0, 2.0],
};
let result = a + b;
println!("Vector3: {:?}", result.elements);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment