In other languages such as in C, you often use a pointer with NULL to indicate that it is not pointing anything. It is not safe to refer it without NULL check.
In Rust, we can use Option<>
. NULL-check is enforced by this type.
Option<T>
allows you to set either a value Some(T)
or None
which is NULL
in C.
Setting a value
You can set a value to Option<T>
type by using Some()
. Use None
to indicate you have no value with None
.
let o = MyObject {value:256};
let p = Some(o);
let q: Option<MyObject> = None;
Non-NULL
There is a unique syntax to let a value to a variable and execute a block only when that was possible.
if let Some(else_branch) = &stmt.else_branch {
self.resolve_statement(else_branch);
}