Card 1 / 12
In Rust, functions are declared with fn. Parameters need explicit types, and the return type follows ->.
fn function_name(param: Type) -> ReturnType {
// body
}The last expression without a semicolon is the return value.
x |
double(x) |
|---|---|
5 |
10 |
0 |
0 |
-3 |
-6 |
To return a value, either use return x; or simply write x (no semicolon) as the last line.
/// Double the input value and return the result
fn double(x: i32) -> i32 {
x * 2
}Card 2 / 12
The modulo operator % returns the remainder of division. A number is even if it's divisible by 2 with no remainder.
Rust's boolean type is bool, with values true and false.