Rust 对 Vec 引用的 Reduce 方法
Rust's Reduce Method on Vec Reference
我正在尝试将 Vec 的引用减少到它的总和,以便我可以计算它的平均值。不过,我 运行 正在处理编译器问题,但我没有关注事情是如何 borrowed/referenced 不正确的。
// Given a list of integers, use a vector and return the mean (the average value), median (when sorted, the value in the middle position), and mode (the value that occurs most often; a hash map will be helpful here) of the list.
fn main() {
let list_of_integers = vec![200, -6_000, 3, 0, 23, 99, -1];
let mean_ans = mean(&list_of_integers);
// Other code that will also use list_of_integers hence why I want to reference list_of_integers so it doesn't get removed from memory
println!("mean is {}", mean_ans);
}
fn mean(integers: &Vec<i32>) -> i32 {
let length = integers.len() as i32;
let sum = integers.iter().reduce(|&a, &b| &(a + b));
match sum {
Some(v) => v / length,
None => 0,
}
}
当我 运行 cargo 运行 并且 rust-analyzer 也将 reduce 方法的 &(a + b) 突出显示为错误时,我收到编译器错误。错误的文本在下面,但我还附上了图片以清楚地显示它所引用的内容。
error[E0515]: cannot return reference to temporary value
--> src\main.rs:13:47
|
13 | let sum = integers.iter().reduce(|&a, &b| &(a + b));
| ^-------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
error: aborting due to previous error
根据我的理解,我不确定这里有什么问题。iter() returns 一个 Iter 对 Vec 的引用,所以它的 a 和 b 的减少值不应该已经是 &i32 了吗?当我从 &(a + b) 中删除 & 时,出现以下编译器错误“expected &i32
, found i32
help: consider borrowing here: &(a + b)
”.
请注意,我只是在学习 Rust,我的教程还不到一半,所以请随意解释解决方案,就好像我是新手一样(因为我是)。
我在 windows 10 in VSCode.
上使用 rust 版本 1.52.1 和 rustup 版本 1.24.1
你这里的问题是传递给 reduce 的函数必须 return 一个与原始迭代器项相同类型的值,对于通过 Iter
特征创建的迭代器,它总是 &T
。但是,您不能 return 来自函数的引用,因为它会指向已释放的堆栈帧。
您的选择是:
- 改为使用
into_iter()
,它消耗调用它的集合并产生拥有的值,
- 使用
iter().cloned()
来克隆值,再次生成一个迭代器来覆盖拥有的值,尽管对于非基本类型来说这可能代价高昂。
但是在总结迭代器的这种特定情况下,您应该只使用 iter().sum()
.
返回 &(a + b)
没有意义,因为它 return 是对闭包本地的临时值的引用,并在其 return 之前销毁,就像本地多变的。解决该问题的最优雅的方法是使用 integers.iter().copied()
获取实际数字而不是引用的迭代器,从而允许您在两个地方都删除 &
。
当您向下传递引用时,像 &(a + b)
这样的表达式很有用,比如传递给需要引用的函数或运算符。在那种情况下,例如对于 { let _tmp = a + b; f(&tmp) }
,f(&(a + b))
是 shorthand,非常合理。
与上述内容无关,请注意您可能希望您的函数接受切片,&[i32]
而不是对向量的引用。这将使用不变的向量,并使您的函数接受其他连续的内存片,例如来自数组。 (有关详细信息,请参阅 。)
我正在尝试将 Vec 的引用减少到它的总和,以便我可以计算它的平均值。不过,我 运行 正在处理编译器问题,但我没有关注事情是如何 borrowed/referenced 不正确的。
// Given a list of integers, use a vector and return the mean (the average value), median (when sorted, the value in the middle position), and mode (the value that occurs most often; a hash map will be helpful here) of the list.
fn main() {
let list_of_integers = vec![200, -6_000, 3, 0, 23, 99, -1];
let mean_ans = mean(&list_of_integers);
// Other code that will also use list_of_integers hence why I want to reference list_of_integers so it doesn't get removed from memory
println!("mean is {}", mean_ans);
}
fn mean(integers: &Vec<i32>) -> i32 {
let length = integers.len() as i32;
let sum = integers.iter().reduce(|&a, &b| &(a + b));
match sum {
Some(v) => v / length,
None => 0,
}
}
当我 运行 cargo 运行 并且 rust-analyzer 也将 reduce 方法的 &(a + b) 突出显示为错误时,我收到编译器错误。错误的文本在下面,但我还附上了图片以清楚地显示它所引用的内容。
error[E0515]: cannot return reference to temporary value
--> src\main.rs:13:47
|
13 | let sum = integers.iter().reduce(|&a, &b| &(a + b));
| ^-------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
error: aborting due to previous error
根据我的理解,我不确定这里有什么问题。iter() returns 一个 Iter 对 Vec 的引用,所以它的 a 和 b 的减少值不应该已经是 &i32 了吗?当我从 &(a + b) 中删除 & 时,出现以下编译器错误“expected &i32
, found i32
help: consider borrowing here: &(a + b)
”.
请注意,我只是在学习 Rust,我的教程还不到一半,所以请随意解释解决方案,就好像我是新手一样(因为我是)。
我在 windows 10 in VSCode.
上使用 rust 版本 1.52.1 和 rustup 版本 1.24.1你这里的问题是传递给 reduce 的函数必须 return 一个与原始迭代器项相同类型的值,对于通过 Iter
特征创建的迭代器,它总是 &T
。但是,您不能 return 来自函数的引用,因为它会指向已释放的堆栈帧。
您的选择是:
- 改为使用
into_iter()
,它消耗调用它的集合并产生拥有的值, - 使用
iter().cloned()
来克隆值,再次生成一个迭代器来覆盖拥有的值,尽管对于非基本类型来说这可能代价高昂。
但是在总结迭代器的这种特定情况下,您应该只使用 iter().sum()
.
返回 &(a + b)
没有意义,因为它 return 是对闭包本地的临时值的引用,并在其 return 之前销毁,就像本地多变的。解决该问题的最优雅的方法是使用 integers.iter().copied()
获取实际数字而不是引用的迭代器,从而允许您在两个地方都删除 &
。
当您向下传递引用时,像 &(a + b)
这样的表达式很有用,比如传递给需要引用的函数或运算符。在那种情况下,例如对于 { let _tmp = a + b; f(&tmp) }
,f(&(a + b))
是 shorthand,非常合理。
与上述内容无关,请注意您可能希望您的函数接受切片,&[i32]
而不是对向量的引用。这将使用不变的向量,并使您的函数接受其他连续的内存片,例如来自数组。 (有关详细信息,请参阅