使用推断(?)类型克隆std :: iter :: Map [英] Cloning a std::iter::Map with inferred (?) type

查看:120
本文介绍了使用推断(?)类型克隆std :: iter :: Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是可以理解的,但如果我尝试使用 time_1s.clone(),我会得到

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()

这也是可以理解的,但在(freq * time_sample).sin() >让foo:f64 在关闭之前返回它没有任何效果。

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.

推荐答案

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

One way to use time_1s twice is to do both together and unzip at the end:

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();
}

这篇关于使用推断(?)类型克隆std :: iter :: Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆