What does Some() do on the left hand side of a variable assignment?

前端 未结 2 610
时光说笑
时光说笑 2021-01-16 17:11

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

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

Inside of this function

f         


        
2条回答
  •  -上瘾入骨i
    2021-01-16 17:56

    The other answers go into a lot of detail, which might be more than you need to know.

    Essentially, this:

    if let Some(path) = env::args().nth(1) {
        // Do something with path
    } else {
        // otherwise do something else
    }
    

    is identical to this:

    match env::args().nth(1) {
        Some(path) => { /* Do something with path */ }
        _          => { /* otherwise do something else */ }
    }
    

提交回复
热议问题