thp-web/md/learn/flow-control/match.md

47 lines
570 B
Markdown
Raw Normal View History

2023-10-02 01:41:38 +00:00
# Match
## Syntax
Braces are **required**.
2023-10-02 01:41:38 +00:00
```thp
2024-02-20 10:17:21 +00:00
val user_id = POST::get("user_id")
2023-10-02 01:41:38 +00:00
match user_id
2024-03-13 17:16:29 +00:00
case Some(id) { print("user_id exists: {id}") }
case None { print("user_id doesn't exist") }
2023-10-02 01:41:38 +00:00
match user_id
2024-03-13 17:16:29 +00:00
case Some(id)
2023-10-02 01:41:38 +00:00
{
print("user_id exists: {id}")
}
2024-03-13 17:16:29 +00:00
case None
2023-10-02 01:41:38 +00:00
{
print("user_id doesn't exist")
}
match user_id
2024-03-13 17:16:29 +00:00
case Some(id) if id > 0
2023-10-02 01:41:38 +00:00
{
print("user_id exists: {id}")
}
2024-03-13 17:16:29 +00:00
else
2023-10-02 01:41:38 +00:00
{
print("user_id has other values ")
}
2024-03-13 17:16:29 +00:00
match customer_id
case 1, 2, 3
{
print("special discount for our first 3 customers!")
}
else
{
print("hello dear")
}
2023-10-02 01:41:38 +00:00
```