("kiam" is "when" in Esperanto)
This crate introduces when! macro which provides better syntax for
if/else if/else chains. The syntax is similar to match.
(idea is borrowed from kotlin)
[]
= "0.1"
Usage
Usage is similar to the usage of match, but instead of patterns, branches are guarded by boolean expression:
when!
_ can be used as a default branch (it's also required to use when! in expression possition):
let x = when! ;
assert_eq!;
You can also use let <pat> = to match a pattern, but in difference with match you'll have to provide an expression for every pattern:
let a = None;
let b = Some;
let fallible = ;
let x = when! ;
assert_eq!;
Last notes:
- You can also compare structure litetals without brackets (you can't do this with
if/else if/elsechain) - You can mixup boolean-braches with pattern matching
- Only one branch is executed (not to be confused with
switchin C-like languages)
let mut x = 0;
when! ;
assert_eq!;
#[derive(PartialEq)]
struct Struct { a: i32 }
// This does not compile because of the ambiguity
if Struct { a: 0 } == Struct { a: 0 } {
// ...
}
when!