Functions vs Closures

Get introduced to functions, closures, and the essence of functional programming.

We'll cover the following...

When new programmers with experiences in other languages begin using Rust, they may be thrown off by the fact that Rust has something like object-oriented programming (OOP)—though it isn’t actually OOP. In fact, Rust leans more towards functional programming (FP), rather than OOP.

Functions are present in all programming languages. However, we’ll focus on Rust’s peculiarities, rather than functions in general. First, the bodies of Rust functions contain instructions and statements, but they can optionally end just with a statement. If they do, the statement is taken as the return of the function. The examples below are all valid functions:

Press + to interact
fn five() -> i32 {
let x = 3;
x + 2
}
fn six() -> i32 {
6
}
fn seven(x: i32) -> i32 {
x - x + 7
}
println!("Function five: {}", five());
println!("Function six: {}", six());
println!("Function seven: {}", seven(5));

Rust has a return keyword, ...