48 lines
1.0 KiB
Plaintext
48 lines
1.0 KiB
Plaintext
;; A kept if let with no else at the end of its chain gives an Option: Some of
|
|
;; the arm that ran, None when none did.
|
|
|
|
fn pick(a: Option(i32), k: i32) -> Option(i32)
|
|
if let Some(x) = a then x
|
|
elif k > 0 then k
|
|
|
|
fn only(a: Option(i32)) -> Option(i32)
|
|
if let Some(x) = a then x * 2
|
|
|
|
fn lead(k: i32, b: Option(i32)) -> Option(i32)
|
|
if k > 5
|
|
k
|
|
elif let Some(y) = b
|
|
y
|
|
|
|
;; An arm that returns stays Never, and the rest of the chain decides.
|
|
fn early(a: Option(i32)) -> Option(i32)
|
|
if let Some(x) = a
|
|
return None
|
|
elif true
|
|
3
|
|
|
|
fn dyn_only(a: Option(i32)) -> dyn
|
|
if let Some(x) = a then x
|
|
|
|
fn show(o: Option(i32))
|
|
match o
|
|
Some(v) -> println(v)
|
|
None -> println("none")
|
|
|
|
fn main()
|
|
show(pick(Some(1), 0))
|
|
show(pick(None, 4))
|
|
show(pick(None, 0))
|
|
show(only(Some(3)))
|
|
show(only(None))
|
|
show(lead(9, None))
|
|
show(lead(1, Some(2)))
|
|
show(lead(1, None))
|
|
show(early(None))
|
|
show(early(Some(1)))
|
|
println(dyn_only(Some(5)))
|
|
println(dyn_only(None))
|
|
;; As a statement it is unchanged.
|
|
if let Some(x) = Some(7)
|
|
println(x)
|