Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Latest commit

 

History

History
53 lines (40 loc) · 1.07 KB

File metadata and controls

53 lines (40 loc) · 1.07 KB

Control Flow

Avoid excessive nesting by using an early return when matching options.

When branching at the end of a function and returning a value, consider using an else block instead of an early return.

Example

Don't do:

if let Some(test) = option {
    // ...

    if check {
        return 15;
    }
    3
} else {
    0
}

Do instead:

let test = match option {
    Some(test) => test,
    None => return 0,
};

// ...

if check {
    15
} else {
    3
}

Explanation

Rust code can easily fall victim to excessive nesting when combining impl blocks, loops and option matching. One of the best ways to avoid this is to extract options and results in match statements and return/break in the None/Err case.

Putting an early return at the end of a function produces inconsistent code which makes it more difficult to follow its control flow. By introducing an else branch, the early return is removed and the branches are unified. However this does introduce another layer of nesting, so the else branch should be simple.