I was reading through the Rust documentation and came across the following example and statement
Using a return as the last line of a function works, but is considered poor style:
fn foo(x: i32) -> i32 {
if x < 5 { return x; }
return x + 1;
}
I know I could have written the above as
fn foo(x: i32) -> i32 {
if x < 5 { return x; }
x + 1
}
but I am more tempted to write the former, as that is more intuitive. I do understand that the function return value should be used as an expression so the later works but then why wouldn't the former be encouraged?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…