Omitting the return keyword

return is for early returns.


The rule is that a block evaluates to an expression if the final expression in the block evaluates to one (and has no semicolon).[1] This is true not just for functions, but any block:

let vec = {
    let mut buf = vec![];
    buf.push(3);
    buf
};

If you have a function like

fn boole(b: bool) -> i32 {
    if b {
        1
    } else {
        0
    }
}

that works because if...else is an expression. (as is if let...else and match). Maybe you might even like to format it as

fn boole(b: bool) -> i32 {
    if b { 1 } else { 0 }
}


  1. To be pedantic, all blocks evaluate to an expression, but those with a trailing semicolon evaluate to (). ↩︎

3 Likes