Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error[E0369]: cannot multiply `f64` by `f64` in Rust

I am a Rust learner. I want to make a function that multiplies an euclid::Vector3D<f64,U> by an f64. I have this code:

use euclid::Vector3D;

fn main() {
    fn scale_vector<f64,Unit>(vec: Vector3D::<f64, Unit>, scale: f64) -> Vector3D::<f64, Unit> {
        Vector3D::<f64, Unit>::new(vec.x * scale, vec.y * scale, vec.z * scale)
    }
    enum Meters {}
    let vec = Vector3D::<f64,Meters>::new(1., 2., 3.);
    let scaled_vec = scale_vector(vec, 2.);

    println!("Original vector: {:?}", vec);
    println!("Scaled vector: {:?}", scaled_vec);
}

but cargo run gives me

error[E0369]: cannot multiply `f64` by `f64`
 --> src/main.rs:5:42
  |
5 |         Vector3D::<f64, Unit>::new(vec.x * scale, vec.y * scale, vec.z * scale)
  |                                    ----- ^ ----- f64
  |                                    |
  |                                    f64
  |
help: consider restricting type parameter `f64`
  |
4 |     fn scale_vector<f64: std::ops::Mul<Output = f64>,Unit>(vec: Vector3D::<f64, Unit>, scale: f64) -> Vector3D::<f64, Unit> {
  |                        +++++++++++++++++++++++++++++

Why? What is the way of doing this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In fn scale_vector(), you added f64 as a generic type. Just remove it and it will compile:

    fn scale_vector<Unit>(vec: Vector3D::<f64, Unit>, scale: f64) -> Vector3D::<f64, Unit> {
        Vector3D::<f64, Unit>::new(vec.x * scale, vec.y * scale, vec.z * scale)
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading