this nice fizzbuzz in rust think:
match (i % 3, % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), _ => println!("{}", i), }
it stated way:
match { if % 3 == 0 && % 5 == 0 => println!("fizzbuzz"), if % 3 == 0 => println!("fizz"), if % 5 == 0 => println!("buzz"), _ => println!("{}", i), }
now confused me.
i if % 3 == 0
is expression, right? so, evaluates i
when condition true. happens if false?
now confused me.
i if % 3 == 0
is expression, right?
no, not. pattern (i
) guard (i % 3 == 0
). maybe got confused because used same bind name. consider modified example:
match { x if x % 3 == 0 && x % 5 == 0 => println!("fizzbuzz"), x if x % 3 => println!("fizz"), x if x % 5 => println!("buzz"), _ => println!("{}", x), }
you can read match expression this
- if
i
matches patternx
(it match ,i
value moved (copied)x
) ,x % 3 == 0
,x % 5 == 0
println!("fizzbuzz")
; else - if
i
matches patternx
,x % 3 == 0
println!("fizz")
; else - if
i
matches patternx
,x % 5 == 0
println!("buzz")
; else println!("{}", x)
Comments
Post a Comment