iter() 和 into_iter() 在共享、借用的 Vec 上的区别?
Difference between iter() and into_iter() on a shared, borrowed Vec?
我正在阅读 Rust 101 tutorial,其中作者以 Vec
对象传递给函数的示例讨论了共享借用。下面是本教程所教内容的稍微改编的 MWE。有趣的部分是 vec_min
中的 v.iter()
。作者写道:
This time, we explicitly request an iterator for the vector v
. The method iter
borrows the vector it works on, and provides shared borrows of the elements.
但是,如果我在共享对象上使用 for ... in ...
结构会怎样?根据 this blog post,此隐式 for 循环使用 into_iter()
,取得 v
的所有权。但它不能真正取得该函数中 v
的所有权,因为它只是从一开始就借用了它,对吗?
有人可以向我解释 into_iter()
和 iter()
之间应用于借用对象的区别吗?
enum NumberOrNothing {
Number(i32),
Nothing,
}
use self::NumberOrNothing::{Number,Nothing};
impl NumberOrNothing {
fn print(self) {
match self {
Nothing => println!("The number is: <nothing>"),
Number(n) => println!("The number is: {}", n),
};
}
}
fn vec_min(v: &Vec<i32>) -> NumberOrNothing {
fn min_i32(a: i32, b: i32) -> i32 {
if a < b {a} else {b}
}
let mut min = Nothing;
for e in v.iter() {
//Alternatively implicitly and with *e replaced by e:
//for e in v {
min = Number(match min {
Nothing => *e,
Number(n) => min_i32(n, *e),
});
}
min
}
pub fn main() {
let vec = vec![18,5,7,2,9,27];
let foo = Nothing;
let min = vec_min(&vec);
let min = vec_min(&vec);
min.print();
}
没有区别。
it cannot really take ownership of the v
in that function, since it has only borrowed it to begin with
绝对可以取得v
的所有权,因为那是&Vec
。请注意此处的精确语义 - 您获得的是引用 的所有权 ,而不是所引用项目的所有权。
如果您查看 implementors of IntoIterator
,您会发现:
impl<'a, T> IntoIterator for &'a Vec<T>
impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> slice::Iter<'a, T> {
self.iter()
}
}
惊喜——它调用 iter
!
同样的逻辑适用于 &mut vec
和 vec.iter_mut()
我正在阅读 Rust 101 tutorial,其中作者以 Vec
对象传递给函数的示例讨论了共享借用。下面是本教程所教内容的稍微改编的 MWE。有趣的部分是 vec_min
中的 v.iter()
。作者写道:
This time, we explicitly request an iterator for the vector
v
. The methoditer
borrows the vector it works on, and provides shared borrows of the elements.
但是,如果我在共享对象上使用 for ... in ...
结构会怎样?根据 this blog post,此隐式 for 循环使用 into_iter()
,取得 v
的所有权。但它不能真正取得该函数中 v
的所有权,因为它只是从一开始就借用了它,对吗?
有人可以向我解释 into_iter()
和 iter()
之间应用于借用对象的区别吗?
enum NumberOrNothing {
Number(i32),
Nothing,
}
use self::NumberOrNothing::{Number,Nothing};
impl NumberOrNothing {
fn print(self) {
match self {
Nothing => println!("The number is: <nothing>"),
Number(n) => println!("The number is: {}", n),
};
}
}
fn vec_min(v: &Vec<i32>) -> NumberOrNothing {
fn min_i32(a: i32, b: i32) -> i32 {
if a < b {a} else {b}
}
let mut min = Nothing;
for e in v.iter() {
//Alternatively implicitly and with *e replaced by e:
//for e in v {
min = Number(match min {
Nothing => *e,
Number(n) => min_i32(n, *e),
});
}
min
}
pub fn main() {
let vec = vec![18,5,7,2,9,27];
let foo = Nothing;
let min = vec_min(&vec);
let min = vec_min(&vec);
min.print();
}
没有区别。
it cannot really take ownership of the
v
in that function, since it has only borrowed it to begin with
绝对可以取得v
的所有权,因为那是&Vec
。请注意此处的精确语义 - 您获得的是引用 的所有权 ,而不是所引用项目的所有权。
如果您查看 implementors of IntoIterator
,您会发现:
impl<'a, T> IntoIterator for &'a Vec<T>
impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> slice::Iter<'a, T> {
self.iter()
}
}
惊喜——它调用 iter
!
同样的逻辑适用于 &mut vec
和 vec.iter_mut()