以下是我的问题的简化/摘要版本:
fn main() {
let arr = vec![1., 2., 3., 4., 5., 6., 7., 8., 9.];
let size = arr.len();
let wave = |res: &Vec<f64>, step| {
let size = res.len();
let factor = size / (2 * step);
res.chunks_exact(2)
.collect::<Vec<_>>()
.chunks_exact(factor)
.flat_map(|arr| {
arr.into_iter().flat_map(|x| {
&[(x[0] + x[1]), (x[0] - x[1])]
})
})
.cloned()
.collect::<Vec<_>>()
};
println!("{:?}", &arr);
let mut res = arr.clone();
for step in 1..size / 2 {
res = wave(&res, step);
println!("{:?}", &res);
}
}
错误:
error[E0515]: cannot return reference to temporary value
--> src/main.rs:34:21
|
34 | &[(x[0] + x[1]), (x[0] - x[1])]
| ^------------------------------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
如果删除.cloned()
方法,但出现下一个问题:
不匹配的类型:
error[E0308]: mismatched types
--> src/main.rs:23:15
|
23 | res = wave(&res, step);
| ^^^^^^^^^^^^^^^^ expected f64, found &f64
|
= note: expected type `std::vec::Vec<f64>`
found type `std::vec::Vec<&f64>`