Functions
202307071413
Status: #idea
Tags: Rust
Functions
- Rust doesn't care about the order in which functions are declared
Info
Rust functions and variables use snake_case
Warning
Parameter types must be explicit declared
fn main() {
print_labeled_measurement(5, 'h');
}
fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value}{unit_label}");
}
Return values
fn five() -> i32 {
5 // Expression (which can always return)
}
;
fn main() {
let x = five();
println!("The value of x is: {x}");
}
fn f(x: i32) -> i32 { x + 1 }
fn main() {
println!("{}", f({
let y = 1;
y + 1
}));
}