Any other reason why we use if-let rather than if in Rust?
Asked Answered
Q

2

23

I don't see the reason why we use if let and just usual if. In Rust book, chapter 6.3, the sample code is below:

let some_u8_value = Some(0u8);
if let Some(3) = some_u8_value {
    println!("three");
}

The code above is same with:

let some_u8_value = Some(0u8);
if Some(3) == some_u8_value {
    println!("three");
}

Any other reason on why we would use if let or what is it specifically for?

Quevedo answered 11/1, 2020 at 7:5 Comment(0)
K
4

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 pattern, an = and a scrutinee expression. If the value of the scrutinee 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.

Source

if let can be used to match any enum value:

enum Foo {
    Bar,
    Baz,
    Qux(u32)
}

fn main() {
    // Create example variables
    let a = Foo::Bar;
    let b = Foo::Baz;
    let c = Foo::Qux(100);

    // Variable a matches Foo::Bar
    if let Foo::Bar = a {
        println!("a is foobar");
    }

    // Variable b does not match Foo::Bar
    // So this will print nothing
    if let Foo::Bar = b {
        println!("b is foobar");
    }

    // Variable c matches Foo::Qux which has a value
    // Similar to Some() in the previous example
    if let Foo::Qux(value) = c {
        println!("c is {}", value);
    }

    // Binding also works with `if let`
    if let Foo::Qux(value @ 100) = c {
        println!("c is one hundred");
    }
}
Koopman answered 11/1, 2020 at 7:10 Comment(1)
very detailed. Thank youSternpost
A
12

Another reason is if you wish to use the pattern bindings. For example consider an enum:

enum Choices {
  A,
  B,
  C(i32),
}

If you wish to implement specific logic for the C variant of Choices, you can use the if-let expression:

let choices: Choices = ...;

if let Choices::C(value) = choices {
    println!("{}", value * 2);
}
Arianaariane answered 11/1, 2020 at 7:52 Comment(0)
K
4

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 pattern, an = and a scrutinee expression. If the value of the scrutinee 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.

Source

if let can be used to match any enum value:

enum Foo {
    Bar,
    Baz,
    Qux(u32)
}

fn main() {
    // Create example variables
    let a = Foo::Bar;
    let b = Foo::Baz;
    let c = Foo::Qux(100);

    // Variable a matches Foo::Bar
    if let Foo::Bar = a {
        println!("a is foobar");
    }

    // Variable b does not match Foo::Bar
    // So this will print nothing
    if let Foo::Bar = b {
        println!("b is foobar");
    }

    // Variable c matches Foo::Qux which has a value
    // Similar to Some() in the previous example
    if let Foo::Qux(value) = c {
        println!("c is {}", value);
    }

    // Binding also works with `if let`
    if let Foo::Qux(value @ 100) = c {
        println!("c is one hundred");
    }
}
Koopman answered 11/1, 2020 at 7:10 Comment(1)
very detailed. Thank youSternpost

© 2022 - 2024 — McMap. All rights reserved.