while と block(オフサイドルール)
この章では、while と block: を使って複数式を1つの流れとして書く方法を学びます。
NEPLg2 では制御構文も式ですが、while は繰り返し本体を do: で与えると読みやすくなります。
while の基本
TEST
#entry main
#indent 4
#target std
#import "core/math" as *
#import "core/result" as *
#import "std/test" as *
fn main <()*>i32> ():
let mut i <i32> 0
let mut sum <i32> 0
while lt i 5:
do:
set sum add sum i
set i add i 1
let checks <Vec<Result<(),str>>>:
checks_new
|> checks_push check_eq_i32 10 sum
let shown <Vec<Result<(),str>>> checks_print_report checks
checks_exit_code shownblock は「最後の式の値」を返す
block: は式なので、let の右辺にも置けます。
TEST
#entry main
#indent 4
#target std
#import "core/math" as *
#import "core/result" as *
#import "std/test" as *
fn main <()*>i32> ():
let x <i32> block:
let a <i32> 3
let b <i32> 4
add a b
let checks <Vec<Result<(),str>>>:
checks_new
|> checks_push check_eq_i32 7 x
let shown <Vec<Result<(),str>>> checks_print_report checks
checks_exit_code shown