{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ownership" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "fn main() {\n", " // Use as many approaches as you can to make it work\n", " let x = String::from(\"Hello world\");\n", " let y = x;\n", " println!(\"{}, {}\", x, y);\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Don't modify code in main!\n", "fn main() {\n", " let s1 = String::from(\"Hello world\");\n", " let s2 = take_ownership(s1);\n", "\n", " println!(\"{}\", s2);\n", "}\n", "\n", "// Only modify the code below!\n", "fn take_ownership(s: String) {\n", " println!(\"{}\", s);\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "fn main() {\n", " let s = give_ownership();\n", " println!(\"{}\", s);\n", "}\n", "\n", "// Only modify the code below!\n", "fn give_ownership() -> String {\n", " let s = String::from(\"Hello world\");\n", " // Convert String to Vec\n", " let _s = s.into_bytes();\n", " s\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Fix the error without removing any code\n", "fn main() {\n", " let s = String::from(\"Hello World\");\n", "\n", " print_str(s);\n", "\n", " println!(\"{}\", s);\n", "}\n", "\n", "fn print_str(s: String) {\n", " println!(\"{}\",s)\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Don't use clone, use copy instead\n", "fn main() {\n", " let x = (1, 2, (), \"hello\".to_string());\n", " let y = x.clone();\n", " println!(\"{:?}, {:?}\", x, y);\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// make the necessary variable mutable\n", "fn main() {\n", " let s = String::from(\"Hello \");\n", " \n", " let s1 = s;\n", "\n", " s1.push_str(\"World!\");\n", "\n", " println!(\"Success!\");\n", "}\n", "\n", "main();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reference & Borrowing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reference" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "fn main() {\n", " let x = 5;\n", " \n", " // Fill the blank\n", " let p = __;\n", " \n", " println!(\"the memory address of x is {:p}\", p); // One possible output: 0x16fa3ac84\n", " }\n", "\n", " main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Fix error\n", "fn main() {\n", " let mut s = String::from(\"hello, \");\n", "\n", " borrow_object(s);\n", "\n", " println!(\"Success!\");\n", "}\n", "\n", "fn borrow_object(s: &String) {}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Fix error\n", "fn main() {\n", " let mut s = String::from(\"hello, \");\n", "\n", " push_str(s);\n", "\n", " println!(\"Success!\");\n", "}\n", "\n", "fn push_str(s: &mut String) {\n", " s.push_str(\"world\")\n", "}\n", "\n", "main();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Borrowing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "// Remove something to make it work\n", "// Don't remove a whole line !\n", "fn main() {\n", " let mut s = String::from(\"hello\");\n", "\n", " let r1 = &mut s;\n", " let r2 = &mut s;\n", "\n", " println!(\"{}, {}\", r1, r2);\n", "\n", " println!(\"Success!\");\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [], "source": [ "fn main() {\n", " // Fix error by modifying this line\n", " let s = String::from(\"hello, \");\n", "\n", " borrow_object(&mut s);\n", "\n", " println!(\"Success!\");\n", "}\n", "\n", "fn borrow_object(s: &mut String) {}\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 }