Learning Rust: Functions and Control Flow
Published at November 20, 2024 by Aulia Rahman
Moving on to functions and control flow. First part was pretty basic, this gets a bit more interesting.
Functions
Basic syntax is familiar:
Interesting how expressions vs statements work - had to fix this in functions4.rs:
Control Flow
if/else is straightforward but no parentheses needed:
Loops are interesting - three types:
The for
loop with iterators is really nice compared to C-style loops.
Notes to Remember
- Function parameters always need type annotations
- Return type uses ->
- Expression vs statement distinction matters
- No parentheses needed in if conditions
- loop can have labels - useful for breaking nested loops
for x in something
is the main loop pattern
Need to practice:
- More complex return types
- Using labeled breaks in nested loops
- Pattern matching with if let
Next: Moving on to Ownership (this is where it gets real...)