使用推断(?)类型克隆std :: iter :: Map

时间:2016-10-10 04:39:09

标签: iterator rust clone type-inference

我无法以紧凑的方式克隆地图:

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内并没有任何效果。

在这样的情况下,我该怎么做?我想做的就是不止一次使用时间向量。

2 个答案:

答案 0 :(得分:7)

两次使用time_1s的一种方法是同时进行并解压缩:

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, sine_100): (Vec<f64>, Vec<f64>) = time_1s.map(|time_sample| {
        ((freq * time_sample).sin(),
         (100.0 * time_sample).sin())
    }).unzip();
}

答案 1 :(得分:3)

我的原始答案引起了3个迭代器的枚举。理想情况下,您正在寻找2次迭代。

由于map使用了迭代器,看起来更简单,更有效的方法是在不引起更多迭代或克隆的情况下执行此操作,只需自己循环一次:

let time_1s = linspace(0.0, 1.0, (freq / 2.0) as usize)
    .map(|sample| { sample * delta});

let mut sine_100 = Vec::new();
let mut sine_440 = Vec::new();

for time_sample in time_1s {
    sine_100.push((100.0 * time_sample).sin());
    sine_440.push((freq * time_sample).sin());
}

println!("{:?}", sine_100);
println!("{:?}", sine_440);