Skip to content

Summing Vec<Complex<T>>: difference between core::num::Zero and num::traits::Zero #126

@SuperFluffy

Description

@SuperFluffy

I wanted to sum a Vec<Complex<T>> using std::iter::Iterator::sum(), which has the signature:

fn sum<S = Self::Item>(self) -> S where S: Add<Self::Item, Output=S> + Zero { ... }

It turns out that the trait core::num::Zero referred to in the signature is different from num::traits::Zero defined in rust-num. You get an error like:

error: the trait `core::num::Zero` is not implemented for the type `num::complex::Complex<f64>` [E0277]

I think it is quite unfortunate that these two traits are not the same. They should be.

A MWE to show the error would be:

use num::complex::Complex;
use num::traits::Zero;

fn main () {
    let cvec = vec![Complex::<f64>::new(1.0,1.0); 10];
    let s = cvec.iter().sum();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions