- 01Setting up (what you'll need)
- 02Your first output (println!)
- 03Variables and mutability (let / mut)
- 04Type annotations and basic types
- 05Branching (the if statement)
- 06Repeating (the for loop)
- 07Writing your own function
- 08Repeating (while and loop)
- 09Ownership, the basics
- 10Borrowing (references, &)
- 11Working with vectors (Vec)
- 12Structs
- 13enum and the match expression
- 14The Option type (null safety)
- 15The Result type and error handling
- 16Traits
- 17Generics
- 18Closures
- 19Working with strings (String)
- 20[Project] Build a small inventory system
Writing your own function
This lesson covers the basic form for defining a function in Rust.
A function is defined as fn name(arg: type) -> return_type { ... }. If the last line of the function is written without a semicolon, it automatically becomes the return value.
The sample code defines fn greet(name: &str) -> String, which takes a name and builds a greeting with the format! macro.
A common early stumble is deciding whether to write return and whether to put a semicolon on the final expression. Leaving the semicolon off makes that value the return value; adding it means the function returns nothing (the () type).
In professional work, getting comfortable with this expression-oriented style is what lets you write concise, idiomatic Rust.
๐งช This site can't compile or run Rust directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
