mercredi 9 décembre 2020

Is it possible to declare a variable in an if expression in Rust?

In Go, we can declare a variable inside an if expression. This variable will valid inside the if scope, and not outside of it. For example:

func main() {
    if n := 4; n != 0 {
        fmt.Printf("%d is not zero", n)
    } else {
        fmt.Printf("%d is zero", n)
    }

    fmt.Printf("%d", n) // error, n doesn't exist here!
}

Is there a similar syntax in Rust?

Aucun commentaire:

Enregistrer un commentaire