fn main() {
// Regular function
// function name: add_one
// function input parameter (x: i32)
// function return value: -> i32
//Regular functions never need a semicolon after their closing brace -
// they are declarations, not statements.
fn add_one(x: i32) -> i32 {
x + 1
}
// Closure that does the same thing
// closure name: add_one_closure
// closure input parameter: |x| //parameter in pipes
//When you assign a closure to a variable using let,
// it is a statement that always needs a semicolon.
let add_one_closure = |x| -> i32 {
x + 1
};
let add_one_closure_v2 = |x| {
x + 1
};
// Both can be called the same way
println!("Function result: {}", add_one(5));
println!("Closure result: {}", add_one_closure(5));
println!("Closure result: {}", add_one_closure_v2(6));
}
To embed this project on your website, copy the following code and paste it into your website's HTML: