Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operations

StaticVector and DynamicVector support the same core set of operations: element-wise addition and subtraction, scalar scaling, the dot product, and the Euclidean norm. The example below constructs two StaticVectors and runs each of them in turn.

#![allow(unused)]
fn main() {
use rustebra::vector::StaticVector;

pub(crate) fn run() {
    println!("== StaticVector ==");
    let a = StaticVector::new([1.0, 2.0, 3.0]);
    let b = StaticVector::new([4.0, 5.0, 6.0]);

    println!("a = {a:?}");
    println!("b = {b:?}");
    println!("a + b = {:?}", a.add(&b));
    println!("a - b = {:?}", a.sub(&b));
    println!("a scaled by 2 = {:?}", a.scale(2.0));
    println!("a . b = {}", a.dot(&b));
    println!("||a|| = {:.4}", a.norm());
}
}

Gotchas

  • add, sub, and dot on StaticVector return a plain value, not a Result — the const generic N guarantees both operands have the same length at compile time, so there’s nothing to fail at runtime. The DynamicVector equivalents return Result<_, LengthMismatch> instead, since two DynamicVectors aren’t guaranteed to match in length.
  • scale takes the factor by value (T, not &T), matching Scalar’s Copy bound — pass the number directly rather than a reference.