Constants and module state
Top-level let and const create compile-time constants and may be public. Top-level let mut creates private mutable module state for the running program.
Modules initialize in dependency order and top-to-bottom. Export functions around mutable state instead of exporting the cell. The concurrency chapter explains what happens when work moves to an isolate.
let NAME = "moon"
let mut count = 0
fn next() -> string {
count += 1
"$NAME $count"
}
fn main() {
println(next())
println(next())
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: moon 1 moon 2
Ctrl/⌘+Enter runs the current lesson.