How to make a function that subtracts two euclid::Vec3D

I am giving my first steps with Rust. I have the following code:

use euclid;

fn main() {
    _7();
}

fn _7() {
    fn minus<T,U>(a: &euclid::Vector3D<T,U>, b: &euclid::Vector3D<T,U>) -> euclid::Vector3D<T,U> {
        let c = a - b;
        c
    }
    enum Unit {}
    let a = euclid::Vector3D::<f64, Unit>::new(1.,0.,0.);
    let b = euclid::Vector3D::<f64, Unit>::new(1.,1.,0.);
    
    let c = a-b; // I know this works.
    
    dbg!(&c);
}

I wrote it this way considering this:

  1. a and b are references, so they are to be borrowed from the "outer scope" and the ownership is not transferred to minus.
  2. c is created inside minus so the ownership is there, but it is then returned so the ownership goes to the "outer scope".

However, this gives me

error[E0369]: cannot subtract `&Vector3D<T, U>` from `&Vector3D<T, U>`

Probably the meaning of this is obvious for someone experienced with Rust, but not for me. How should I implement such function?

(I know there is already an implementation of this, but wanna do it to gain understanding, I am playing around.)

You'll need to look at the trait bounds for the impls of Vector3D for Copy and Sub. An impl is only applicable if and only if its trait bounds are met.

You didn't post the whole error message, or maybe you're getting it from an IDE. Always run cargo build in the terminal and post the whole thing. Looks like it is useful.

error[E0369]: cannot subtract `&Vector3D<T, U>` from `&Vector3D<T, U>`
  --> src/lib.rs:25:27
   |
25 |                 let c = a - b;
   |                         - ^ - &Vector3D<T, U>
   |                         |
   |                         &Vector3D<T, U>
   |
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
   |
24 |             ) -> euclid::Vector3D<T, U> where &Vector3D<T, U>: Sub<&Vector3D<T, U>> {
   |                                         +++++++++++++++++++++++++++++++++++++++++++

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.