|
|
|
|
|
by falcolas
4155 days ago
|
|
So I should use `return` except when I shouldn't? This is the cognitive overhead problem I'm talking about. The original context of my concern is the instance where the match statement makes up the last statement in the function (frequently the only statement in the function's immediate scope). Since the individual cases are not terminating the function early, to get a value out of a match statement you simply leave the last expression bare. i.e. fn something... {
match input {
Ok(input_val) => {/* several lines of semicolon terminated code*/
output}
Err(errval) => {/* several more lines of semicolon terminated code */
output}
}
is correct, but fn something... {
match input {
Ok(input_val) => {/* several lines of semicolon terminated code*/
return output;}
Err(errval) => {/* several more lines of semicolon terminated code */
return output;}
}
is poor style, and fn something... {
match input {
Ok(input_val) => {/* several lines of semicolon terminated code*/
output;}
Err(errval => {/* several more lines of semicolon terminated code */
output;}
}
is an error. |
|
No. Use `return` only when you must. If you want an early return in a function, then you need to use `return`. If you don't need an early return, then don't use `return` at all.
I can't remember if this was ever a cognitive load for me personally. I don't think it was.