num := 1
match num % 2 == 0 {
true { print('The input number is even.') }
else { print('The input number is odd.') }
}
num := 3
s := match num {
1 { 'one' }
2 { 'two' }
else {
'many'
}
}
fn even(num int) bool {
match num % 2 == 0 {
true { print('The input number is even.') }
else { print('The input number is odd.') }
}
}
fn num_to_str(num int) int {
match num {
1 { 'one' }
2 { 'two' }
else {
'many'
}
}
}
fn main() {
println(even(14)) // 'The input number is even.'
println(even(3)) // 'The input number is odd.'
println(num_to_str(1)) // 'one'
println(num_to_str(2)) // 'two'
println(num_to_str(352)) // 'many'
}
enum Animal {
cat
dog
goldfish
pig
}
fn makes_miau(a Animal) bool {
return match a {
.cat { true } // enumの`cat`
else { false }
}
}
fn is_land_creature(a Animal) bool {
return match a {
.cat { true } // enumの`cat`
.dog { true } // enumの`dog`
.pig { true } // enumの`pig`
else {
false
}
}
}
// 以下の書き方も可能
fn is_land_creature_alt(a Animal) bool {
return match a {
.goldfish { false } // enumの`goldfish`
else {
true
}
}
}
fn main() {
my_cat := Animal.cat
my_goldfish := Animal.goldfish
println(makes_miau(my_cat)) // true
println(is_land_creature(my_cat)) // true
println(is_land_creature(my_goldfish)) // false
}