Some() 在变量赋值的左侧做什么? [英] What does Some() do on the left hand side of a variable assignment?

查看:46
本文介绍了Some() 在变量赋值的左侧做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些 Rust 代码,我遇到了这一行

I was reading some Rust code and I came across this line

if let Some(path) = env::args().nth(1) {

这个函数的内部

fn main() {
    if let Some(path) = env::args().nth(1) {
        // Try reading the file provided by the path.
        let mut file = File::open(path).expect("Failed reading file.");
        let mut content = String::new();
        file.read_to_string(&mut content);

        perform_conversion(content.as_str()).expect("Conversion failed.");
    } else {
        println!(
            "provide a path to a .cue file to be converted into a MusicBrainz compatible tracklist."
        )
    }
}

该行似乎将 env 参数分配给变量路径,但我无法弄清楚它周围的 Some() 在做什么.

The line seems to be assigning the env argument to the variable path but I can't work out what the Some() around it is doing.

我查看了Option<的文档/code> 并且我理解在 = 的右侧使用它是如何工作的,但在左侧我有点困惑.

I took a look at the documentation for Option and I understand how it works when used on the right hand side of = but on the left hand side I am a little confused.

我认为这条线等同于

if let path = Some(env::args().nth(1)) {

推荐答案

来自 参考 :

if let 表达式在语义上类似于 if 表达式,但是代替条件表达式,它需要关键字 let 跟随通过一个可反驳的模式,一个 = 和一个表达式.如果该值= 右侧的表达式与模式匹配,将执行相应的块,否则流程继续执行如果存在,则跟随 else 块.就像 if 表达式,if let表达式的值由被评估的块决定.

An if let expression is semantically similar to an if expression but in place of a condition expression it expects the keyword let followed by a refutable pattern, an = and an expression. If the value of the expression on the right hand side of the = matches the pattern, the corresponding block will execute, otherwise flow proceeds to the following else block if it exists. Like if expressions, if let expressions have a value determined by the block that is evaluated.

这里的重要部分是可反驳性.这里的可反驳模式是什么意思,它可以有不同的形式.例如:

In here the important part is refutability. What it means refutable pattern in here it can be in different forms. For example :

enum Test {
    First(String, i32, usize),
    Second(i32, usize),
    Third(i32),
}

您可以检查 3 种不同模式的值的 x 值,例如:

You can check the x's value for a value for 3 different pattern like :

fn main() {
    let x = Test::Second(14, 55);
    if let Test::First(a, b, c) = x {}
    if let Test::Second(a, b) = x {} //This block will be executed
    if let Test::Third(a) = x {}
}

这称为反驳性.但请像这样考虑您的代码:

This is called refutability. But consider your code like this:

enum Test {
    Second(i32, usize),
}

fn main() {
    let x = Test::Second(14, 55);
    if let Test::Second(a, b) = x {}
}

这段代码不会编译,因为 x 的模式很明显,它只有一个模式.您可以从 反驳参考.

This code will not compile because x's pattern is obvious, it has single pattern. You can get more information from the reference of refutability.

你的想法也不对:

if let path = Some(env::args().nth(1)) {

编译器会抛出类似 irrefutable if-let 模式 的错误,因为正如参考文献所说:关键字 let 后跟一个可反驳的模式".在这里,让"之后没有可反驳的模式.实际上,这段代码试图创建一个名为 path 的变量,它是一个 Option,这是没有意义的,因为不需要If",

Compiler will throw error like irrefutable if-let pattern because as the reference says: "keyword let followed by a refutable pattern". In here there is no refutable pattern after "let". Actually this code tries to create a variable named path which is an Option and this make no sense because there is no "If" needed,

相反,Rust 希望你这样写:

Instead Rust expects from you to write like this:

let path = Some(env::args().nth(1)); // This will be seem like Some(Some(value))

这篇关于Some() 在变量赋值的左侧做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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