英文:
Does the `map` call serve any purpose here?
问题
以下是翻译好的内容:
看起来从 这里 引用的代码如下:
pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)> + '_ {
self.values.iter()
}
有什么不同吗?
英文:
Code:
pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)> + '_ {
self.values.iter().map(|(key, value)| (key, value))
}
Quoted from here, it seems following is enough:
pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)> + '_ {
self.values.iter()
}
Any difference?
答案1
得分: 2
是的,前者返回一个类型,它实现了 Iterator<Item = (&usize, &T)> + '_
,而后者返回一个类型,它实现了 Iterator<Item = &(usize, T)> + '_
。
这里是一个最小的示例:
pub fn iter1(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
v.iter().map(|(key, value)| (key, value))
}
pub fn iter2(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
v.iter()
}
使用这个错误信息编译失败:
error[E0271]: expected `std::slice::Iter<'_, (usize, i32)>` to be an iterator that yields `(&usize, &i32)`, but it yields `&(usize, i32)`
--> src/lib.rs:5:37
|
5 | pub fn iter2(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&(usize, i32)`, found tuple
6 | v.iter()
| -------- return type was inferred to be `std::slice::Iter<'_, (usize, i32)>` here
|
= note: expected reference `&(usize, i32)`
found tuple `(&usize, &i32)`
有关此错误的更多信息,请尝试 `rustc --explain E0271`。
error: could not compile `playground` due to previous error
前者之所以能工作,是因为 Rust 的模式语法中内置了一种称为 绑定模式 的符号糖:
当引用值与非引用模式匹配时,它将自动被视为 ref 或 ref mut 绑定。
因此,它基本上是 v.iter().map(|&(ref key, ref value)| (key, value))
的语法糖,这使得前者为什么有效更加清晰,但后者为什么无效。
英文:
Yes, the former returns a type that implements Iterator<Item = (&usize, &T)> + '_
while the latter returns a type that implements Iterator<Item = &(usize, T)> + '_
.
Here a minimal example:
pub fn iter1(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
v.iter().map(|(key, value)| (key, value))
}
pub fn iter2(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
v.iter()
}
fails to compile with this error message:
error[E0271]: expected `std::slice::Iter<'_, (usize, i32)>` to be an iterator that yields `(&usize, &i32)`, but it yields `&(usize, i32)`
--> src/lib.rs:5:37
|
5 | pub fn iter2(v: &[(usize, i32)]) -> impl Iterator<Item = (&usize, &i32)> + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&(usize, i32)`, found tuple
6 | v.iter()
| -------- return type was inferred to be `std::slice::Iter<'_, (usize, i32)>` here
|
= note: expected reference `&(usize, i32)`
found tuple `(&usize, &i32)`
For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` due to previous error
The former works thanks to ergonomics build into Rust's pattern syntax called binding modes:
> When a reference value is matched by a non-reference pattern, it will be automatically treated as a ref or ref mut binding.
So it is basically syntactic sugar for v.iter().map(|&(ref key, ref value)| (key, value))
, which makes it clearer why the former works, but the latter doesn't.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论