在 for 循环中引用/取消引用向量元素
在下面的代码中,我想在迭代之后保留 number_list
,因为 for
使用的 .into_iter()
默认会消耗.因此,我假设 n: &i32
并且我可以通过取消引用获得 n
的值.
In the code below, I want to retain number_list
, after iterating over it, since the .into_iter()
that for
uses by default will consume. Thus, I am assuming that n: &i32
and I can get the value of n
by dereferencing.
fn main() {
let number_list = vec![24, 34, 100, 65];
let mut largest = number_list[0];
for n in &number_list {
if *n > largest {
largest = *n;
}
}
println!("{}", largest);
}
有人告诉我,我们可以使用 &n
作为模式"来代替这个:
It was revealed to me that instead of this, we can use &n
as a 'pattern':
fn main() {
let number_list = vec![24, 34, 100, 65];
let mut largest = number_list[0];
for &n in &number_list {
if n > largest {
largest = n;
}
}
println!("{}", largest);
number_list;
}
我的困惑(请记住我没有涵盖模式)是我希望因为 n: &i32
,然后 &n: &&i32
而不是它解析为值(如果双引用甚至可能).为什么会发生这种情况,&
的含义是否因上下文而异?
My confusion (and bear in mind I haven't covered patterns) is that I would expect that since n: &i32
, then &n: &&i32
rather than it resolving to the value (if a double ref is even possible). Why does this happen, and does the meaning of &
differ depending on context?
将引用视为一种容器会有所帮助.为了进行比较,请考虑 Option
,我们可以在其中使用模式匹配解开"值,例如在 if let
语句中:
It can help to think of a reference as a kind of container. For comparison, consider Option
, where we can "unwrap" the value using pattern-matching, for example in an if let
statement:
let n = 100;
let opt = Some(n);
if let Some(p) = opt {
// do something with p
}
我们为 Option
调用 Some
和 None
构造函数,因为它们每个都会产生一个 选项代码>.同样,您可以将
&
视为引用的构造函数.并且语法是对称的:
We call Some
and None
constructors for Option
, because they each produce a value of type Option
. In the same way, you can think of &
as a constructor for a reference. And the syntax is symmetric:
let n = 100;
let reference = &n;
if let &p = reference {
// do something with p
}
您可以在将值绑定到变量的任何地方使用此功能,这种情况随处可见.例如:
You can use this feature in any place where you are binding a value to a variable, which happens all over the place. For example:
if let
,同上
匹配
表达式:
match opt {
Some(1) => { ... },
Some(p) => { ... },
None => { ... },
}
match reference {
&1 => { ... },
&p => { ... },
}
在函数参数中:
In function arguments:
fn foo(&p: &i32) { ... }
循环:
Loops:
for &p in iter_of_i32_refs {
...
}
可能还有 更多.
请注意,最后两个对 Option
不起作用,因为如果找到 None
而不是 Some
,它们会恐慌,但是引用不会发生这种情况,因为它们只有一个构造函数,&
.
Note that the last two won't work for Option
because they would panic if a None
was found instead of a Some
, but that can't happen with references because they only have one constructor, &
.
&
的含义是否因上下文而异?
does the meaning of
&
differ depending on context?
希望,如果您可以将 &
解释为构造函数而不是运算符,那么您会发现它的含义不会改变.Rust 的一个非常酷的特性是,您可以在表达式的右侧使用构造函数来创建值,在左侧使用构造函数将它们分开(解构).
Hopefully, if you can interpret &
as a constructor instead of an operator, then you'll see that its meaning doesn't change. It's a pretty cool feature of Rust that you can use constructors on the right hand side of an expression for creating values and on the left hand side for taking them apart (destructuring).