3 ms·
The only one that seems like a real problem is // this is a common pattern in rust for one-line matches. If this thing matches Some(..) // we can do so
by sundarurfriend 2y ago
The only one that seems like a real problem is
// this is a common pattern in rust for one-line matches. If this thing matches Some(..)
// we can do something with the string.
if let Some(username) = maybe_username {
// so ok, this code will run if the username exists...
return username.to_uppercase();
}
// except, uh... Now that code will only run if 'username' matches Some("hey")
const username: &str = "hey";
As convenient as hoisting sometimes can be, I'm wary of it for exactly reasons like this. Even in "normal" code flow, it disrupts the usual linear flow of code logic when you're reading it, and this kind of action-at-a-distance makes it especially bad.