2023-11-18 05:44:28 +03:00
|
|
|
# Functions.
|
|
|
|
|
|
|
|
# Declare.
|
|
|
|
|
|
|
|
Declare functions using `fn` keyword.
|
|
|
|
|
|
|
|
```rust
|
2023-11-18 09:54:14 +03:00
|
|
|
// Simple function.
|
2023-11-18 05:44:28 +03:00
|
|
|
fn main() {}
|
|
|
|
|
2023-11-18 09:54:14 +03:00
|
|
|
// With argument of type i32.
|
2023-11-18 05:44:28 +03:00
|
|
|
fn main(x: i32) {}
|
2023-11-18 09:54:14 +03:00
|
|
|
|
|
|
|
// With return type of i32.
|
|
|
|
fn main() -> i32 {}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Return value.
|
|
|
|
|
|
|
|
## Return keyword.
|
|
|
|
|
|
|
|
You can return value by writing `return` keyword. Use this in multiline functions.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
fn foo() -> i32
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Last expression.
|
|
|
|
|
|
|
|
It is also possible to return the result of the last line by removing `;` at the end. Use this for single-line functions.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
fn foo() -> i32 { 10 }
|
2023-11-18 05:44:28 +03:00
|
|
|
```
|