Why does Rust need the `if let` syntax?

前端 未结 4 1354
渐次进展
渐次进展 2020-12-18 21:39

Coming from other functional languages (and being a Rust newbie), I\'m a bit surprised by the motivation of Rust\'s if let syntax. The RFC mentions that without

4条回答
  •  一生所求
    2020-12-18 21:56

    Because your solution creates a closure, which uses resources, whereas if let desugars exactly to your first example, which doesn't. I also find it more readable.

    Rust is all about zero-cost abstractions that make programming nicer, and if let and while let are good examples of those (at least IMO -- I realize it's a matter of personal preference). They're not strictly necessary, but they sure feel good to use (also see: Clojure, where they were likely lifted from).

提交回复
热议问题