442 lines
8.4 KiB
Text
442 lines
8.4 KiB
Text
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Struct"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"1."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fix the error\n",
|
||
"struct Person {\n",
|
||
" name: String,\n",
|
||
" age: u8,\n",
|
||
" hobby: String\n",
|
||
"}\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" let age = 30;\n",
|
||
" let p = Person {\n",
|
||
" name: String::from(\"sunface\"),\n",
|
||
" age,\n",
|
||
" };\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"2."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fix the errors and fill the blanks\n",
|
||
"struct Color(i32, i32, i32);\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" let v = (0, 127, 255);\n",
|
||
" check_color(v);\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"fn check_color(p: Color) {\n",
|
||
" let (x, _, _) = p;\n",
|
||
" assert_eq!(x, 0);\n",
|
||
" assert_eq!(p.1, 127);\n",
|
||
" assert_eq!(__, 255);\n",
|
||
" }\n",
|
||
"\n",
|
||
" main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"3."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"\n",
|
||
"// Fill the blank and fix the error without adding/removing new line\n",
|
||
"struct Person {\n",
|
||
" name: String,\n",
|
||
" age: u8,\n",
|
||
"}\n",
|
||
"fn main() {\n",
|
||
" let age = 18;\n",
|
||
" let p = Person {\n",
|
||
" name: String::from(\"john\"),\n",
|
||
" age,\n",
|
||
" };\n",
|
||
"\n",
|
||
" // How can you believe sunface is only 18? \n",
|
||
" p.age = 30;\n",
|
||
"\n",
|
||
" // Fill the blank\n",
|
||
" __ = String::from(\"John\");\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"}\n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"4."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fill the blank to make the code work\n",
|
||
"struct User {\n",
|
||
" active: bool,\n",
|
||
" username: String,\n",
|
||
" email: String,\n",
|
||
" sign_in_count: u64,\n",
|
||
"}\n",
|
||
"fn main() {\n",
|
||
" let u1 = User {\n",
|
||
" email: String::from(\"someone@example.com\"),\n",
|
||
" username: String::from(\"sunface\"),\n",
|
||
" active: true,\n",
|
||
" sign_in_count: 1,\n",
|
||
" };\n",
|
||
"\n",
|
||
" let u2 = set_email(u1);\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"fn set_email(u: User) -> User {\n",
|
||
" User {\n",
|
||
" email: String::from(\"contact@im.dev\"),\n",
|
||
" __\n",
|
||
" }\n",
|
||
"}\n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"5."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fill the blanks to make the code work\n",
|
||
"#[__]\n",
|
||
"struct Rectangle {\n",
|
||
" width: u32,\n",
|
||
" height: u32,\n",
|
||
"}\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" let scale = 2;\n",
|
||
" let rect1 = Rectangle {\n",
|
||
" width: dbg!(30 * scale), // Print debug info to stderr and assign the value of `30 * scale` to `width`\n",
|
||
" height: 50,\n",
|
||
" };\n",
|
||
"\n",
|
||
" dbg!(&rect1); // Print debug info to stderr\n",
|
||
"\n",
|
||
" println!(__, rect1); // Print debug info to stdout\n",
|
||
"}\n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Enum"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"1."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fix the errors\n",
|
||
"enum Number {\n",
|
||
" Zero,\n",
|
||
" One,\n",
|
||
" Two,\n",
|
||
"}\n",
|
||
"\n",
|
||
"enum Number1 {\n",
|
||
" Zero = 0,\n",
|
||
" One,\n",
|
||
" Two,\n",
|
||
"}\n",
|
||
"\n",
|
||
"// C-like enum\n",
|
||
"enum Number2 {\n",
|
||
" Zero = 0,\n",
|
||
" One = 1,\n",
|
||
" Two = 2,\n",
|
||
"}\n",
|
||
"\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" // An enum variant can be converted to a integer by `as`\n",
|
||
" assert_eq!(Number::One, Number1::One);\n",
|
||
" assert_eq!(Number1::One, Number2::One);\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"2."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fill in the blank\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 msg1 = Message::Move{__}; // Instantiating with x = 1, y = 2 \n",
|
||
" let msg2 = Message::Write(__); // Instantiating with \"hello, world!\"\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"3."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"\n",
|
||
"// Fill in the blank and fix the error\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 msg = Message::Move{x: 1, y: 2};\n",
|
||
"\n",
|
||
" if let Message::Move{__} = msg {\n",
|
||
" assert_eq!(a, b);\n",
|
||
" } else {\n",
|
||
" panic!(\"NEVER LET THIS RUN!\");\n",
|
||
" }\n",
|
||
"\n",
|
||
" println!(\"Success!\");\n",
|
||
"} \n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"4."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"// Fill in the blank and fix the errors\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",
|
||
"\n",
|
||
"fn show_message(msg: Message) {\n",
|
||
" println!(\"{}\", msg);\n",
|
||
"}\n",
|
||
"\n",
|
||
"main();"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Impl"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"1. Implement functions for `struct`"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "rust"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"struct ContactEntry(String, String);\n",
|
||
"\n",
|
||
"// TODO: implement functions for ContactEntry\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" let c1 = ContactEntry::new(\"John\", \"666-66-66-66\");\n",
|
||
" \n",
|
||
" assert_eq!(c1.name(), \"John\");\n",
|
||
" assert_eq!(c1.phone(), \"666-66-66-66\"); \n",
|
||
"}"
|
||
]
|
||
}
|
||
],
|
||
"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
|
||
}
|