4

I am trying to perform a sum over a vector, but I keep getting the error,

error[E0277]: the trait bound `T: Sum<&T>` is not satisfied
    --> src\equations\equation.rs:86:45
     |
86   |                 a[i][j][0] = a[i][j].iter().sum();
     |                                             ^^^ the trait `Sum<&T>` is not implemented for `T`
     |
note: required by a bound in `std::iter::Iterator::sum`
    --> C:\Users\Utilizador\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\core\src\iter\traits\iterator.rs:2985:12
     |
2985 |         S: Sum<Self::Item>,
     |            ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
help: consider further restricting this bound
     |
39   |         T: Float + AddAssign + DivAssign + Sum + std::iter::Sum<&T>

As mentioned in the error, T is a generic type that implements Float from the num-traits crate.

This is part of a larger Finite Volume code I am implementing from scratch, and I don't really know what can be actually relevant to put here, but this operation is performed inside a trait with the following method implementation:

fn compute_final_ap(&mut self) {
    let i0: usize = *self.get_i0();
    let iend: usize = *self.get_iend();
    let j0: usize = *self.get_j0();
    let jend: usize = *self.get_jend();

    let mut a: &mut Vec<Vec<Vec<T>>> = self.get_a_matrix();

    for i in i0..iend {
       for j in j0..jend {
           a[i][j][0] = - a[i][j][0];
           a[i][j][0] = a[i][j].iter().sum();
       }
    }
}

I know this is terrible code, but it's just to speed up the code implementation for now. I want to implement a sparse matrix for a later on.

I also tried to use into_iter().sum() but was not capable of using it since it complains with the error move occurs because value has type Vec<T>, which does not implement the Copy trait.

Hope this information is enough for you to help me with the issue. Thanks.

0 Answers0