[Beginner] Adding Vectors

⚓ Rust    📅 2026-06-29    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: [Beginner] Adding Vectors

Vec<T> is more general here than a mathematics 1D vector.

But when T is a number type I would expect Add to be implemented in it.

Already fearing I would face the orphan rule since both items were non-local (I wasn't certain though), I tried to use something like this:

use core::ops::Add;

fn main() {
    impl Add<u32> for Vec<u32> {
      ......
    }
}

Which of course failed.

I double checked my remaining options by looking at the time crate implementing Add, which uses locally-defined type, and the a local trait in Unicode Segmentation.

So I guess my question would be, why was this operation left out of vectors? Was it just a hassle to implement?

And, in this case, is a best practice to define a VectorAdd trait, or a new MathVector type? (This is for learning purposes, but I am open to look at existing libraries for inspiration, if there is any you'd suggest.)

I suppose there may not be a simple answer but am curious how others think about it.

PS: I just realised that implementing Add to MathVector would conserve the use of +. So that's one difference.

7 posts - 5 participants

Read full topic

🏷️ Rust_feed