Clarification on conditional assignment

Clarification on conditional assignment
Hi everyone! I hope I don't come of as oblivious. I'm currently following the Interactive Rust Book and one of the quizzes presented in the topic of Control Flow in Rust is this: let x; if cond { x = 1; } else { x = 2; } Although it was explained briefly like this: Note that Rust does not require x to be initially declared with let mut in the second snippet. This is because Rust can determine that x is only ever assigned once, since only one branch of the if-statement will ever execute. My question then is, how come this is valid Rust code, and x is assigned a value while we didn't declare x to be mutable? Isn't semicolon terminates the previous expression and that the next one is ready to begin? 4 posts - 4 participants Read full topic

Take Your Experience to the Next Level

New

Download our mobile app for a faster and better experience.

Comments

0
U

Join the discussion

Sign in to leave a comment

0:000:00