Functions in Rust
Functions are central in Rust. We’ll learn about functions with a few different examples.
To run the program:
There are several other features to Rust functions. One is multiple return values, which we’ll look at next.
Key differences from the original:
- In Rust, the
main
function is defined without parameters. - Rust uses
println!
macro instead of fmt.Println
. - Rust function declarations start with
fn
instead of func
. - Rust uses
->
to specify the return type, placed after the parameter list. - Rust allows implicit returns for the last expression in a function if you omit the semicolon.
- In Rust, you need to specify the type for each parameter. There’s no shorthand for multiple parameters of the same type.
- Rust uses
i32
as the default integer type, similar to int
in the original. - Variable declarations in Rust use
let
keyword.
The structure and explanation have been maintained, but adapted to Rust’s syntax and conventions.