title | slug |
---|---|
Functions |
functions |
- Named functions are declared with the keyword
fn
- When using arguments, you must declare the data types.
- By default, functions return an empty tuple/
()
. If you want to return a value, the return type must be specified after->
fn main() {
println!("Hello, world!");
}
fn print_sum(a: i8, b: i8) {
println!("sum is: {}", a + b);
}
// 01. Without the return keyword. Only the last expression returns.
fn plus_one(a: i32) -> i32 {
a + 1
// There is no ending ; in the above line.
// It means this is an expression which equals to `return a + 1;`.
}
// 02. With the return keyword.
fn plus_two(a: i32) -> i32 {
return a + 2;
// Should use return keyword only on conditional/ early returns.
// Using return keyword in the last expression is a bad practice.
}
fn main() {
// 01. Without type declarations.
let p1 = plus_one;
let x = p1(5); // 6
// 02. With type declarations.
let p1: fn(i32) -> i32 = plus_one;
let x = p1(5); // 6
}
fn plus_one(a: i32) -> i32 {
a + 1
}
- Also known as anonymous functions or lambda functions.
- The data types of arguments and returns are optional ⃰ⁱᵛ.
Example with a named function, before using closures.
fn main() {
let x = 2;
println!("{}", get_square_value(x));
}
fn get_square_value(i: i32) -> i32 {
i * i
}
fn main() {
let x = 2;
let square = |i: i32| -> i32 { // Input parameters are passed inside | | and expression body is wrapped within { }
i * i
};
println!("{}", square(x));
}
fn main() {
let x = 2;
let square = |i| i * i; // { } are optional for single-lined closures
println!("{}", square(x));
}
fn main() {
let x = 2;
let x_square = |i: i32| -> i32 { i * i }(x); // { } are mandatory while creating and calling same time.
println!("{}", x_square);
}
fn main() {
let x = 2;
let x_square = |i| -> i32 { i * i }(x); // ⭐️ The return type is mandatory.
println!("{}", x_square);
}