keyword

Rust return: exit a function early

Use `return` to finish a function immediately. It is helpful when an exceptional or simple case can be handled before the remaining work. Expected output: 0 5 Why: Orders of at least 50 return zero before reaching the final expression. Common mistake: The return value must agree with the function return type.

Formal Syntax & Structure

rust syntaxrust
return expression;

Working Example

Example Snippetrust
fn shipping(total: u32) -> u32 {    if total >= 50 {        return 0;    }    5}
fn main() {    println!("{}", shipping(70));    println!("{}", shipping(20));}
Rust Reference