如何更正第2节中的语法错误?显示错误消息
error[E0277] slice indices are of type `usize` or ranges of `usize`
第 2 节编译时。 (第 1 部分和第 2 部分;两者在不同的编程风格中具有相同的功能。)
extern crate rand;
use std::iter;
use rand::Rng;
fn estimateAreaCircleMonteCarlo() {
let box_area = 4.0;
let numIteration = 100000;
let mut x = Vec::<f64>::with_capacity(numIteration);
let mut y = Vec::<f64>::with_capacity(numIteration);
let mut distance = Vec::<f64>::with_capacity(numIteration);
let mut rng = rand::thread_rng();
x = iter::repeat(())
.map(|()| rng.gen_range(-1., 1.))
.take(numIteration)
.collect::<Vec<f64>>();
y = iter::repeat(())
.map(|()| rng.gen_range(-1., 1.))
.take(numIteration)
.collect::<Vec<f64>>();
// section 1, it is able to be compiled.
distance = (0..numIteration)
.map(|i| (x[i].powi(2) + y[i].powi(2)).sqrt())
.collect::<Vec<f64>>();
// section 2, same functionality as section 1 but it cannot be compiled.
// error[E0277]^^^^ slice indices are of type `usize` or ranges of `usize`
/*
distance = iter::repeat(())
.map(|i| (x[i].powi(2) + y[i].powi(2)).sqrt())
.take(numIteration)
.collect::<Vec<f64>>();
*/
println!("x={}", x[numIteration - 1]);
println!("y={}", y[numIteration - 1]);
println!("distance={}", distance[numIteration - 1]);
}
最佳答案
iter::repeat(())
创建一个产生 ()
(单元类型)的迭代器。然后你用这个闭包映射那些迭代的项目:
|i| (x[i].powi(2) + y[i].powi(2)).sqrt()
i
因此属于 ()
类型。正如编译器所说,您不能使用 ()
索引某些内容:
the trait `std::slice::SliceIndex<[f64]>` is not implemented for `()`
解决这个问题的一种方法是枚举:
distance = iter::repeat(()).enumerate()
.map(|(i, _)| (x[i].powi(2) + y[i].powi(2)).sqrt())
.take(numIteration)
.collect();
但是你的第一个解决方案更好,因为 iter::repeat
东西没用。
请注意,如果您不需要保留 x
和 y
,则会进行一些不必要的分配。这段代码可以简单地写成:
extern crate rand;
use rand::Rng;
fn estimate_area_circle_monteCarlo() {
let num_iteration = 100000;
let mut rng = rand::thread_rng();
let distances: Vec<_> = (0..num_iteration)
.map(|_| {
let x: f32 = rng.gen_range(-1., 1.);
let y: f32 = rng.gen_range(-1., 1.);
(x.powi(2) + y.powi(2)).sqrt()
})
.collect();
println!("last distance = {}", distances.last().unwrap());
}
仅当您确实需要 Vec
、HashSet
或其他任何内容时才使用 collect
。如果您的目的是迭代某些数据,请保持迭代器不变。
关于lambda - 用 rust 编写的替代功能样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50443267/