Rust functions: Difference between revisions
From wikinotes
No edit summary |
|||
Line 29: | Line 29: | ||
fn main() i32 { // returns 123 | fn main() i32 { // returns 123 | ||
123 // <-- no semicolon | 123 // <-- return value (no semicolon) | ||
} | } | ||
Revision as of 16:58, 7 February 2023
Expressions vs Statements
- statements include actions without a return value (ends in ;)
- expressions include actions with a return value (no ;)
statement
{ let y = 1; y += 1; } // no return valexpression
let x = { let y = 1; y += 1 // <-- no semicolon } // returns 2
Function Signatures
fn main() { // returns void println!("hi"); } fn main() i32 { // returns 123 123 // <-- return value (no semicolon) } fn main(num: u8) { // param type println!("{num}"); }