这段代码之所以有效,是因为
然而,如果我将
Iterator
实现了rev()
方法,其中self
是DoubleEndedIterator
类型。let vec: Vec<i32> = Vec::new();
for x in vec.iter().rev() {
//Do stuff
}
然而,如果我将
vec.iter().rev()
更改为&vec.rev()
,它将无法编译,因为:
no method named `rev` found for type `std::vec::Vec<i32>` in the current scope
此外:
the method `rev` exists but the following trait bounds were not satisfied: `std::vec::Vec<i32> : std::iter::Iterator`, `[i32] : std::iter::Iterator`
但是 for 循环隐式调用 IntoIterator
呢?&vec
或 vec.iter()
被认为是 Rust 中的惯用语吗?