A calculation becomes easier to reuse when it has a name, an input and a result. Rust functions declare parameter types inside parentheses and a return type after an arrow.[1] We will use one to add one to two different numbers.
Pass a value, keep the result
These examples were tested on Linux ARM64 with rustc 1.99.1 and Cargo 1.99.1. Create a fresh project, then open its functions folder in your editor and terminal.
cargo new --vcs none functions
$ cargo new --vcs none functions Creating binary (application) `functions` packagenote: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.htmlexit=0
Replace src/main.rs with this complete programme.
fn add_one(value: i32) -> i32 { value + 1}fn main() { let first = add_one(4); let second = add_one(9); println!("First: {first}"); println!("Second: {second}");}
In add_one, value is the parameter name and i32 is its type. The arrow followed by i32 declares the type of result.[1] Both calls supply a small integer; the function evaluates the same calculation for each input.
The final line, value + 1, is an expression. It produces a value, which becomes the function’s result.[1] Leave this line without a semicolon. The let statements in main store each returned value, then println! displays it.
Run the programme from the functions folder. Each terminal capture includes the executed command and its exit status.
cargo run --quiet
$ cargo run --quietFirst: 5Second: 10exit=0
Our two calls returned 5 and 10. The calculation lives in one place, while each call supplies its own input. We use small i32 values here to keep the example focused on passing and returning values.

A common error: one extra semicolon
Replace src/main.rs with this deliberately failing version. Only the calculation’s ending has changed.
fn add_one(value: i32) -> i32 { value + 1;}fn main() { let first = add_one(4); let second = add_one(9); println!("First: {first}"); println!("Second: {second}");}
Adding a semicolon turns that expression into a statement. This function body then yields the unit value, written (), although its signature promises i32.[1] Check the programme without building an executable.[3]
cargo check --quiet
$ cargo check --quieterror[E0308]: mismatched types --> src/main.rs:1:27 |1 | fn add_one(value: i32) -> i32 { | ------- ^^^ expected `i32`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression2 | value + 1; | - help: remove this semicolon to return this valueFor more information about this error, try `rustc --explain E0308`.error: could not compile `functions` (bin "functions") due to 1 previous errorexit=101
Our compiler reported E0308, expected i32, found (). E0308 means the expected and received types differ.[2] Remove the semicolon after value + 1 and restore the working programme.
Exercise
Rename the function add_two, update both calls, and change the calculation to value + 2. Run it again. Expect First: 6 and Second: 11.
Next: use if and else to choose a result based on a condition.
Browse Rust, one step at a time
Leave a Reply