Control Flow
Flint keeps control flow conventional.
Conditionals
Use if and else for branching:
if ready {
return ok(())
} else {
return err(Error.NotReady)
}
Loops
Flint currently supports while and loop as the stable loop forms:
mut i = 0
while i < 10 {
i = i + 1
}
loop {
break
}
while is the obvious bounded loop. loop is the obvious infinite loop.
The parser already accepts for i in 0..10, but range-based for lowering is still being finished. Until that lands end to end, the book treats while and loop as the supported control-flow loops.
Match
match handles tagged unions and structured branching:
match state {
State.Idle => { return }
State.Busy(job) => { return run(job) }
}
Use match when the shape of a value should drive control flow.
For the common “one pattern or fall back” case, Flint also supports if let:
if let Option.Some(job) = next_job() {
run(job)
} else {
return
}
Defer
defer schedules cleanup for function exit:
fn work(bus: spi.Bus) -> Result<(), Error> {
let device = bus.init()?
defer device.release()
return ok(())
}
Deferred actions run in last-in, first-out order when the function returns.