Let expression: Are we ready for another RFC?

This style is reviewed in if-let-chain rfc and they mentioned two problems, it isn't short circuited and it can't depend on previous chains: (example borrowed from that rfc)

assert!((let Ok(user) = read_user(::std::io::stdin()))
    && user.name == "Alan Turing"
    && (let Ok(hobby) = read_hobby_of(&user))
    && hobby == "Hacking Enigma");

So if let and if and if let && let are three different animals in your point of view? They will be the same after let expression:

  • let is an bool expression which return true if it matches the pattern.
  • if is something that get a bool and if it is true run the first block and otherwise run the else block (and similar declaration for while)
  • if compiler can figure out a let expression is statically true in some context (by breaking && (and in future || and !) and by knowing the definition of if) it will let you access variable binded by that let expression.

Which is more clear? This view or syntax sugar of match for each case? New learners already expect anything inside if to be a bool expression. For example here a new learner wrotes if (let Some(x) = y) and this thread OP mentions this problem. Also n that thread there is a good discussion around let expression which @Centril explains it better than me.

1 Like