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.
fn sign(n: i32) -> &'static str { if n == 0 { return "zero"; } if n < 0 { "negative" } else { "positive" } }
Try it
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.
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
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
returnonly for early exits. Clippy'sneedless_returnlint flags an unnecessary finalreturn. - 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 anasyncblock it leaves that block. - The
?operator isreturnin disguise: onErrorNoneit returns early from the function, converting the error withFrom. See the ? operator. maincan return aResult:fn main() -> Result<(), Box<dyn Error>>; returningErrprints the error withDebugand 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 areturnat all (for example one that loops forever or exits the process).