`&` 在循环变量之前的目的是什么? [英] What is the purpose of `&` before the loop variable?

查看:29
本文介绍了`&` 在循环变量之前的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码&i in list&的作用是什么?如果我删除 &,它会在 largest = i 中产生错误,因为它们的类型不匹配(其中 i&32ii32).但是&i是如何将i转换成i32的呢?

fn large(list: &[i32]) ->i32 {println!("{:?}", list);让 mut 最大 = 列表 [0];对于列表中的 &i {如果我>最大的{最大的 = i;}}最大的}fn 主(){let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32];println!("最大的数是:{}", maximum(&hey));}

游乐场

它似乎以某种方式取消引用,但是为什么在下面的代码中,它不起作用?

fn main() {让 mut 嘿: i32 = 32;让 x: i32 = 2;嘿 = &&x;}

它说:

4 |嘿 = &&x;|^^^ 预期为 i32,找到 &&i32|= 注意:预期类型`i32`找到类型`&&i32`

解决方案

所以通常当你使用 for i in list 时,循环变量 i 的类型是 <代码>&i32.

但是当您使用 for &i in list 时,您不会取消引用任何东西,而是您使用模式匹配来显式解构引用,这将使 i 成为 i32 类型.

参见 Rust 文档关于 for-loop 循环变量 作为一个模式参考模式我们在这里使用.另请参阅关于 解构指针.

 

解决此问题的另一种方法是保持 i 原样,然后将 i 与对 largest 的引用进行比较,然后在分配给 largest 之前取消引用 i:

fn maximum(list: &[i32]) ->i32 {println!("{:?}", list);让 mut 最大 = 列表 [0];对于列表中的 i {如果我>&最大的{最大的 = * i;}}最大的}

 

<小时><块引用>

fn main() {让 mut 嘿: i32 = 32;让 x: i32 = 2;嘿 = &&x;}

这根本行不通,因为在这里您将hey(一个i32)分配给对i32 的引用的引用代码>.这与循环变量情况下的模式匹配和解构完全无关.

What is the purpose of & in the code &i in list? If I remove the &, it produces an error in largest = i, since they have mismatched types (where i is &32 and i is i32). But how does &i convert i into i32?

fn largest(list: &[i32]) -> i32 {
    println!("{:?}", list);
    let mut largest = list[0];
    for &i in list {
        if i > largest {
            largest = i;
        }
    }
    largest
}

fn main() {
    let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32];
    println!("The largest number is: {}", largest(&hey));
}

Playground

It seems like it is somehow dereferencing, but then why in the below code, is it not working?

fn main() {
    let mut hey: i32 = 32;
    let x: i32 = 2;
    hey = &&x;
}

It says:

4 |     hey = &&x;
  |           ^^^ expected i32, found &&i32
  |
  = note: expected type `i32`
             found type `&&i32`

解决方案

So normally when you use for i in list, the loop variable i would be of type &i32.

But when instead you use for &i in list, you are not dereferencing anything, but instead you are using pattern matching to explicitly destructure the reference and that will make i just be of type i32.

See the Rust docs about the for-loop loop variable being a pattern and the reference pattern that we are using here. See also the Rust By Example chapter on destructuring pointers.

 

An other way to solve this, would be to just keep i as it is and then comparing i to a reference to largest, and then dereferencing i before assigning to largest:

fn largest(list: &[i32]) -> i32 {
    println!("{:?}", list);
    let mut largest = list[0];
    for i in list {
        if i > &largest {
            largest = *i;
        }
    }
    largest
}

 


fn main() {
    let mut hey: i32 = 32;
    let x: i32 = 2;
    hey = &&x;
}

This simply doesn't work, because here you are assigning hey, which is an i32, to a reference to a reference to an i32. This is quite unrelated to the pattern matching and destructuring in the loop variable case.

这篇关于`&amp;` 在循环变量之前的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆