Error message blames wrong line

Actual error is in the line before the last one: let x4: Vec<f64> = x3.collect(); but compiler blames the last one, where it makes no sense, because I have specified explicitly type of x4. Is this a bug in type resolver?

fn main() {
    let v = vec![1_f64, 2.2_f64];
    let mut fft: Vec<Vec<f64>> = vec![];

    let x1: &[f64] = &v;
    let x2 = x1.into_iter();
    let x3 = x2.rev();
    let x4: Vec<f64> = x3.collect();    // <-- mistake is here
    fft.push(x4);                                  // <-- but error blames this line
}
 --> src/main.rs:9:14
  |
9 |     fft.push(x4);
  |              ^^ a collection of type `std::vec::Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`

And while we are on this, what is idiomatic way to copy slice in reverse order into vector?

This definitely looks like an error reporting bug to me. I've tried to find an existing issue documenting it, but I don't think there are any (or at least, none with this exact error). Could you possibly report this as a bug to https://github.com/rust-lang/rust/issues/?

As for copying a reverse slice, your way seems pretty good. I'd probably reach for [T]::to_vec and Vec::reverse, but both methods seem pretty obvious and without benchmarks it's hard to say if one would be any faster.

Done: Error message blames wrong line · Issue #66923 · rust-lang/rust · GitHub

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.