rust / beginner
Snippet
Defining Basic Functions
Functions are declared using 'fn'. Parameters must have explicit type annotations, and the return type is specified after an arrow '->'. The last expression in a function is its return value.
snippet.rs
1
2
3
4
5
6
7
8
fn add(a: i32, b: i32) -> i32 {a + b}fn main() {let sum = add(5, 10);println!("The sum is: {sum}");}
Breakdown
1
fn add(a: i32, b: i32) -> i32 {
Defines function 'add' taking two 32-bit integers and returning one.
2
a + b
The return expression. Note the absence of a semicolon.