Optional values
Optional<T> is either some(value) or none; T? is equivalent type shorthand. none needs context from an annotation, branch, argument, or return type.
Match both cases for explicit control. Inside a function returning another optional, postfix ? unwraps some or returns none early.
fn label(name: Optional<string>) -> string {
match name {
some(value) => value,
none => "unknown",
}
}
fn main() {
println(label(some("Ada")))
println(label(none))
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: Ada unknown
Ctrl/⌘+Enter runs the current lesson.