【发布时间】:2017-02-18 11:14:11
【问题描述】:
我无法以紧凑的方式克隆地图:
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();
}
我用这段代码得到的错误是
`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
这是可以理解的,但如果我尝试改用time_1s.clone(),我会得到
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()
这也是可以理解的,但是在返回之前将 (freq * time_sample).sin() 存储在闭包内的 let foo: f64 中没有任何效果。
在这种情况下我该怎么办?我只想多次使用时间向量。
【问题讨论】:
标签: iterator rust clone type-inference