I'm having trouble cloning a Map in a compact way:
extern crate itertools_num;
use itertools_num::linspace;
fn main() {
// 440Hz as wave frequency (middle A)
let freq: f64 = 440.0;
// Time vector sampled at 880 times/s (~Nyquist), over 1s
let delta: f64 = 1.0 / freq / 2.0;
let time_1s = linspace(0.0, 1.0, (freq / 2.0) as usize)
.map(|sample| { sample * delta});
let sine_440: Vec<f64> = time_1s.map(|time_sample| {
(freq * time_sample).sin()
}).collect();
let sine_100: Vec<f64> = time_1s.map(|time_sample| {
(100.0 * time_sample).sin()
}).collect();
}
The error I get with this code is
`time_1s` moved here because it has type `std::iter::Map<itertools_num::Linspace<f64>, [closure@examples/linear_dft.rs:12:14: 12:40 delta:&f64]>`, which is non-copyable
which is understandable, but if I try to use time_1s.clone()
instead, I get
note: the method `clone` exists but the following trait bounds were not satisfied: `[closure@examples/linear_dft.rs:12:14: 12:40 delta:_] : std::clone::Clone`
error: the type of this value must be known in this context
(freq * time_sample).sin()
which is also understandable, but storing (freq * time_sample).sin()
in a let foo: f64
inside the closure before returning it doesn't have any effect.
What am I supposed to do in a situation like this? All I wanted to do was use the time vector more than once.