mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 13:06:22 +00:00
1,018 B
1,018 B
id | title |
---|---|
1324afaa-085a-401c-9757-df93b6c51423 | Rust functions |
Functions
Case
Rust uses snake case for function and variable names. Functions always start with fn
fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
Parameters
In rust you must declare parameter types
fn main() {
another_function(5);
}
fn another_function(x: i32) {
println!("The value of x is: {}", x);
}
Rust is expressive
Rust is an expressive language, so you have to do stuff like this:
fn main() {
let _x = 5;
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {}", y);
}
Return values
Unless you add a return statement, most functions return the last expression implicitly:
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {}", x);
}