Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Keywords and Statements

The return keyword

Reference for Rust return: returning early from functions and closures, the implicit value of the last expression, the never type and the ? operator.

return expr; ends the current function (or closure) immediately and hands expr back to the caller. In Rust you rarely need it at the end of a function, because a function body is a block and the last expression without a semicolon is its value. return is for leaving early: guard clauses, error cases and the inside of nested loops.

A return expression itself has type ! (never), because control never continues past it. That is why it is accepted in places where another type is expected, such as an arm of a match or the else block of a let ... else.

Rust
fn sign(n: i32) -> &'static str {
    if n == 0 { return "zero"; }
    if n < 0 { "negative" } else { "positive" }
}

Try it

Rust Early return and implicit value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust return in closures, match arms and with ?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
return;              // in a function returning ()
return expression;
fn name() -> Type { ...; value }   // implicit return
Form Description
return expr Evaluates expr, runs destructors of local variables, and exits the function. The type of expr must match the declared return type.
return Shorthand for return ();.
Final expression The last expression of the body, without a semicolon, is the return value.

Errors: returning the wrong type is E0308 ("mismatched types"). Adding a stray semicolon after the last expression makes the body return () and triggers the same error with a hint to remove it.

Usage notes

  • Idiomatic style: use a trailing expression for the normal result and return only for early exits. Clippy's needless_return lint flags an unnecessary final return.
  • Guard clauses keep nesting shallow: check bad input first and return, then write the main logic unindented.
  • Closures have their own return; it leaves the closure, not the surrounding function. Inside an async block it leaves that block.
  • The ? operator is return in disguise: on Err or None it returns early from the function, converting the error with From. See the ? operator.
  • main can return a Result: fn main() -> Result<(), Box<dyn Error>>; returning Err prints the error with Debug and exits with code 1.
  • Drop order: locals are dropped when you return, in reverse declaration order, after the return value has been computed.
  • No multiple returns in the sense of tuples-as-syntax: return a tuple (a, b) or a struct.
  • Functions that never return are declared -> ! and do not need a return at all (for example one that loops forever or exits the process).