trainings/Rust_2026/rust-bs-2026-09-14/exercises/rust-exercises/5_pattern-match.ipynb

213 lines
4.2 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Match"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Fill the blanks\n",
"enum Direction {\n",
" East,\n",
" West,\n",
" North,\n",
" South,\n",
"}\n",
"\n",
"fn main() {\n",
" let direction = Direction::South;\n",
" match direction {\n",
" Direction::East => println!(\"East\"),\n",
" __ => { // Matching South or North here\n",
" println!(\"South or North\");\n",
" },\n",
" _ => println!(__),\n",
" };\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Match is an expression, so we can use it in assignments"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fn main() {\n",
" let boolean = true;\n",
"\n",
" // Fill the blank with a match expression:\n",
" //\n",
" // boolean = true -> binary = 1\n",
" // boolean = false -> binary = 0\n",
" let binary = __;\n",
"\n",
" assert_eq!(binary, 1);\n",
"\n",
" println!(\"Success!\");\n",
"}\n",
"\n",
" main();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Using match to get the data an enum variant holds."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Fill in the blanks\n",
"enum Message {\n",
" Quit,\n",
" Move { x: i32, y: i32 },\n",
" Write(String),\n",
" ChangeColor(i32, i32, i32),\n",
"}\n",
"\n",
"fn main() {\n",
" let msgs = [\n",
" Message::Quit,\n",
" Message::Move{x:1, y:3},\n",
" Message::ChangeColor(255,255,0)\n",
" ];\n",
"\n",
" for msg in msgs {\n",
" show_message(msg)\n",
" }\n",
"\n",
" println!(\"Success!\");\n",
"} \n",
"\n",
"fn show_message(msg: Message) {\n",
" match msg {\n",
" __ => { // match Message::Move\n",
" assert_eq!(a, 1);\n",
" assert_eq!(b, 3);\n",
" },\n",
" Message::ChangeColor(_, g, b) => {\n",
" assert_eq!(g, __);\n",
" assert_eq!(b, __);\n",
" }\n",
" __ => println!(\"no data in these variants\")\n",
" }\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If let"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fn main() {\n",
" let o = Some(7);\n",
"\n",
" // Remove the whole `match` block, using `if let` instead \n",
" match o {\n",
" Some(i) => {\n",
" println!(\"This is a really long string and `{:?}`\", i);\n",
"\n",
" println!(\"Success!\");\n",
" }\n",
" _ => {}\n",
" };\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Fill in the blank\n",
"enum Foo {\n",
" Bar(u8)\n",
"}\n",
"\n",
"fn main() {\n",
" let a = Foo::Bar(1);\n",
"\n",
" __ {\n",
" println!(\"foobar holds the value: {}\", i);\n",
"\n",
" println!(\"Success!\");\n",
" }\n",
"}\n",
"\n",
"main();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "rust",
"pygment_lexer": "rust",
"version": ""
}
},
"nbformat": 4,
"nbformat_minor": 2
}