{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0d7300a-edd5-4eac-9260-b5af6f17e5c2",
   "metadata": {},
   "source": [
    "# PLC2026 Lecture 8, 3 Feb 2026"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ed093b-e17f-40bd-b42b-71cfb3b25898",
   "metadata": {},
   "source": [
    "### Strings\n",
    "- A string created by assigning a constant is allocated on the strack:\n",
    "\n",
    "  `String s = \"hello\";`\n",
    "  \n",
    "- A string constructed constructed using `String::from()` is stored on the heap. Such strings are mutable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83190f61-a6ac-49bf-bfa8-e310fcde0775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n",
      "Again hello, world!\n"
     ]
    }
   ],
   "source": [
    "let mut s = String::from(\"hello\"); // allocates heap space for new String and initializes\n",
    "s.push_str(\", world!\"); // push_str() appends a literal to a String\n",
    "println!(\"{}\", s); // This will print `hello, world!`\n",
    "println!(\"Again {}\",s);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84b6d417-3f57-42b1-9754-53801e9c7390",
   "metadata": {},
   "source": [
    "### Assignment copies values, stack\n",
    "- For values on the stack, assignment copies the value to the new location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "70980753-b77b-48aa-9e43-6e6d212a43ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 7, y = 77\n"
     ]
    }
   ],
   "source": [
    "let mut x = 7;\n",
    "let mut y = x;\n",
    "y = 77;\n",
    "println!(\"x = {}, y = {}\",x,y);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "217225f8-96c1-47e3-bf80-785319dd516e",
   "metadata": {},
   "source": [
    "### Assignment *moves* values, heap\n",
    "- Every value on the heap has a unique owner\n",
    "- Assignment *moves* ownership\n",
    "- Memory is freed as soon as scope of owner ends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e359975c-7683-4ba2-a292-7dd1f2ed53d7",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "borrow of moved value: `s1`",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0382] Error:\u001b[0m borrow of moved value: `s1`",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_4:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m1 │\u001b[0m \u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68mm\u001b[0m\u001b[38;5;68mu\u001b[0m\u001b[38;5;68mt\u001b[0m\u001b[38;5;68m \u001b[0m\u001b[38;5;68ms\u001b[0m\u001b[38;5;68m1\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mS\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249mf\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m┬\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m        \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m move occurs because `s1` has type `String`, which does not implement the `Copy` trait",
      " \u001b[38;5;246m2 │\u001b[0m \u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54m1\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;37m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m               \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m value moved here",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;37m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;37m╰\u001b[0m\u001b[38;5;37m─\u001b[0m help: consider cloning the value if the performance cost is acceptable: `.clone()`",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m4 │\u001b[0m \u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m1\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;100m1\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                              \u001b[38;5;100m─\u001b[0m\u001b[38;5;100m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                               \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m value borrowed here after move",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "let mut s1 = String::from(\"hello\");\n",
    "let mut s2 = s1;\n",
    "s2.push_str(\", world\");\n",
    "println!(\"s1 = {}, s2 = {}\", s1, s2); "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73a870dd-7af6-433f-8353-8e286cf2a6f8",
   "metadata": {},
   "source": [
    "### The `Copy` trait\n",
    "- Traits are Rust's equivalent of Java interfaces and Python type classes\n",
    "- For type that have `Copy` trait, values are copied without moving ownership\n",
    "- All scalar types have this trait: `u16`, `i32`, `f64`, `bool`, `char` etc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0653063c-535a-442e-89d9-48fec208225d",
   "metadata": {},
   "source": [
    "### Mutable parameters\n",
    "- Need to declare `mut` to update in function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53319c3b-ce19-439e-8066-910030aa7b57",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot assign to immutable argument `x`",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0384] Error:\u001b[0m cannot assign to immutable argument `x`",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_5:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m7 │\u001b[0m \u001b[38;5;249mf\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249mx\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249m3\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m{\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m           \u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m           \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m help: consider making this binding mutable: `mut `",
      " \u001b[38;5;246m8 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54m=\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54m+\u001b[0m\u001b[38;5;54m5\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m        \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m cannot assign to immutable argument",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m \u001b[38;5;115mNote\u001b[0m: You can change an existing variable to mutable like: `let mut x = x;`",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "fn main(){\n",
    "    let mut y = 77;\n",
    "    update(y);\n",
    "    println!(\"y is {}\",y);\n",
    "}\n",
    "\n",
    "fn update(x:i32){\n",
    "    x = x+5;\n",
    "    println!(\"x is {}\",x);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bce9f73b-b503-43c0-b5a6-e7bc54580c71",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main(){\n",
    "    let mut y = 77;\n",
    "    update(y);\n",
    "    println!(\"y is {}\",y);\n",
    "}\n",
    "\n",
    "fn update(mut x:i32){\n",
    "    x = x+5;\n",
    "    println!(\"x is {}\",x);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ccd7dabd-e8bf-43a3-a86a-3924ba90988d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 82\n",
      "y is 77\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fb5a31c-21cf-4968-9766-7c79600e58b6",
   "metadata": {},
   "source": [
    "### Cloning\n",
    "- Makes a copy of a heap value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b9c2f944-212b-43de-9d48-5617a61dc417",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1 = hello, s2 = hello, world\n"
     ]
    }
   ],
   "source": [
    "let mut s1 = String::from(\"hello\");\n",
    "let mut s2 = s1.clone();\n",
    "s2.push_str(\", world\");\n",
    "\n",
    "println!(\"s1 = {}, s2 = {}\", s1, s2);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a7f58d0-70a6-43e7-b89e-915f5e8cff34",
   "metadata": {},
   "source": [
    "### Transferring ownership via function calls\n",
    "- Move heap values back and forth"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d98c2c7-843c-4ff2-b3d2-9cf7e6101460",
   "metadata": {},
   "source": [
    "- Here, ownership of `s` moves to function `takes_ownership`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ef143c90-d95b-46c7-88dc-e72351678154",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let s = String::from(\"hello\");  // s comes into scope\n",
    "\n",
    "    takes_ownership(s);             // s's value moves into function...\n",
    "                                    // ... no longer valid here\n",
    "} // s goes out of scope. Since s's value was moved, nothing special happens.\n",
    "\n",
    "fn takes_ownership(some_string: String) { // some_string comes into scope\n",
    "    println!(\"{}\", some_string);\n",
    "} // some_string goes out of scope, `drop` is called, memory is freed\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ad4ea255-0f09-4245-887b-051ccfa0d5ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aecf4798-f14f-4ef8-9183-a86b837818c4",
   "metadata": {},
   "source": [
    "If we try to use `s` after its value has moved, we get an error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b183acd4-5eae-4719-852c-ce7515554227",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "borrow of moved value: `s`",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0382] Error:\u001b[0m borrow of moved value: `s`",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_11:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m2 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68ms\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mS\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249mf\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mc\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mc\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249me\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m         \u001b[38;5;68m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m         \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m move occurs because `s` has type `String`, which does not implement the `Copy` trait",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m4 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mk\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m_\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mw\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m'\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mv\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mv\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mf\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mc\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m.\u001b[0m\u001b[38;5;249m.\u001b[0m\u001b[38;5;249m.\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                     \u001b[38;5;54m┬\u001b[0m\u001b[38;5;37m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                     \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m value moved here",
      " \u001b[38;5;240m  │\u001b[0m                      \u001b[38;5;37m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                      \u001b[38;5;37m╰\u001b[0m\u001b[38;5;37m─\u001b[0m help: consider cloning the value if the performance cost is acceptable: `.clone()`",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m6 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                   \u001b[38;5;100m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                   \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m value borrowed here after move",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "fn main() {\n",
    "    let s = String::from(\"hello\");  // s comes into scope\n",
    "\n",
    "    takes_ownership(s);             // s's value moves into function...\n",
    "                                    // ... no longer valid here\n",
    "    println!(\"{}\",s);\n",
    "} // s goes out of scope. Since s's value was moved, nothing special happens.\n",
    "\n",
    "fn takes_ownership(some_string: String) { // some_string comes into scope\n",
    "    println!(\"{}\", some_string);\n",
    "} // some_string goes out of scope, `drop` is called, memory is freed\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0808bec8-67d7-49c5-891d-66926044e4dc",
   "metadata": {},
   "source": [
    "- For types with `Copy` trait, the value is copied to the function without moving ownership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5cb94f43-f9a7-48a1-8953-08b513abc51e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let s = String::from(\"hello\");  // s comes into scope\n",
    "\n",
    "    takes_ownership(s);             // s's value moves into function...\n",
    "                                    // ... no longer valid here\n",
    "\n",
    "    let x = 5;                      // x comes into scope\n",
    "\n",
    "    makes_copy(x);                  // x would move into the function, but\n",
    "    println!(\"x is {}\",x);          // i32 is Copy, so okay to still use x\n",
    "} // x goes out scope, then s.\n",
    "  // Since s's value was moved, nothing special happens.\n",
    "\n",
    "fn takes_ownership(some_string: String) { // some_string comes into scope\n",
    "    println!(\"{}\", some_string);\n",
    "} // some_string goes out of scope, `drop` is called, memory is freed\n",
    "\n",
    "fn makes_copy(some_integer: i32) { // some_integer comes into scope\n",
    "    println!(\"{}\", some_integer);\n",
    "} // some_integer goes out of scope, nothing special happens.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fdf40bf8-86cc-44cd-aa10-16eec2338149",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "5\n",
      "x is 5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ea7e239-8958-4e0a-be5b-484424721abb",
   "metadata": {},
   "source": [
    "- Examples of moving heap values in and out of functions\n",
    "- In `gives_ownership`, the scope of `some_string` ends but the value created is moved to the calling scope by the return and hence persists after the function exits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b3cceadb-2720-4492-9396-29a33995bacc",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let s1 = gives_ownership();         // gives_ownership moves its return\n",
    "                                        // value into s1\n",
    "\n",
    "    let s2 = String::from(\"hello\");     // s2 comes into scope\n",
    "\n",
    "    let s3 = takes_and_gives_back(s2);  // s2 is moved into\n",
    "                                        // takes_and_gives_back, which also\n",
    "                                        // moves its return value into s3\n",
    "} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing\n",
    "  // happens. s1 goes out of scope and is dropped.\n",
    "\n",
    "fn gives_ownership() -> String {             // gives_ownership will move its\n",
    "                                             // return value into the function\n",
    "                                             // that calls it\n",
    "\n",
    "    let some_string = String::from(\"yours\"); // some_string comes into scope\n",
    "\n",
    "    some_string                              // some_string is returned and\n",
    "                                             // moves out to the calling\n",
    "                                             // function\n",
    "}\n",
    "\n",
    "// This function takes a String and returns one\n",
    "fn takes_and_gives_back(a_string: String) -> String { // a_string comes into\n",
    "                                                      // scope\n",
    "\n",
    "    a_string  // a_string is returned and moves out to the calling function\n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1b3fa175-6c13-4cd3-a2c6-1220b8b8865f",
   "metadata": {},
   "outputs": [],
   "source": [
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed303016-9e34-4bc0-87a3-ee5a1fd7ef49",
   "metadata": {},
   "source": [
    "- Transferring ownership requires clumsy mechanisms to \"get back\" parameters passed to functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fab49d83-49a9-4252-9a58-3ec00a5feab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let s1 = String::from(\"hello\");\n",
    "    let (s2, len) = calculate_length(s1);\n",
    "    println!(\"The length of '{}' is {}.\", s2, len);\n",
    "}\n",
    "\n",
    "fn calculate_length(s: String) -> (String, usize) {\n",
    "    let length = s.len(); // len() returns the length of a String\n",
    "    (s, length)\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "686cea22-1025-40b1-9d75-fec6bbd9fcd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The length of 'hello' is 5.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9efab826-5fb1-45ce-8453-e1f8f3c659de",
   "metadata": {},
   "source": [
    "### References\n",
    "- Point to a variable that contains a value on the heap\n",
    "- Avoids moving ownership\n",
    "- Creating a reference results in *borrowing* the value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1130726e-903a-4823-aefb-bef5827f21f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let s1 = String::from(\"hello\");\n",
    "    let len = calculate_length(&s1);\n",
    "    println!(\"The length of '{}' is {}.\", s1, len);\n",
    "}\n",
    "\n",
    "fn calculate_length(s: &String) -> usize {\n",
    "    s.len()\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "567489ee-072d-479a-8131-335474aca838",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The length of 'hello' is 5.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca55887f-3dda-478b-ba8f-9df01aa3fcdb",
   "metadata": {},
   "source": [
    "- Arguments passed as references are not automatically mutable\n",
    "- Use `&mut` to denote a mutable reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcc52222-2cf8-4af2-8414-6361bec7448c",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot borrow `*some_string` as mutable, as it is behind a `&` reference",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0596] Error:\u001b[0m cannot borrow `*some_string` as mutable, as it is behind a `&` reference",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_20:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m6 │\u001b[0m \u001b[38;5;249mf\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mc\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mm\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m_\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m&\u001b[0m\u001b[38;5;249mS\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                         \u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                         \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m help: consider changing this to be a mutable reference: `mut `",
      " \u001b[38;5;246m7 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54m_\u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mr\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54mg\u001b[0m\u001b[38;5;249m.\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249m_\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mw\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m          \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m `some_string` is a `&` reference, so it cannot be borrowed as mutable",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m \u001b[38;5;115mNote\u001b[0m: You can change an existing variable to mutable like: `let mut x = x;`",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "fn main() {\n",
    "    let s = String::from(\"hello\");\n",
    "    change(&s);\n",
    "}\n",
    "\n",
    "fn change(some_string: &String) {\n",
    "    some_string.push_str(\", world\");\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fe97f8ee-e277-434f-8d52-4e1e3f448a44",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let mut s = String::from(\"hello\");\n",
    "    change(&mut s);\n",
    "    println!(\"s is {}\",s);\n",
    "}\n",
    "\n",
    "fn change(some_string: &mut String) {\n",
    "    some_string.push_str(\", world\");\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "834b0f9c-5480-47b6-91d3-8fdc93b1aec7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s is hello, world\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb6068b1-8ba0-4d40-832f-b69dc7004111",
   "metadata": {},
   "source": [
    "### Constraints on mutable references\n",
    "- One mutable reference is permitted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e7a72b46-820d-497b-bae7-79d940605760",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "    let r1 = &mut s;\n",
    "    println!(\"{}\", r1);\n",
    "    println!(\"{}\", s);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9977a600-fc43-42a5-8fdb-1fa1965a946a",
   "metadata": {},
   "source": [
    "- But now only the reference can update the value, not the original variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10118bb9-d609-43a2-a567-0521795bc94a",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot borrow `s` as mutable more than once at a time",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0499] Error:\u001b[0m cannot borrow `s` as mutable more than once at a time",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_24:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m3 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m1\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54m&\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54mu\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m first mutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m5 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;249m.\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249m_\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;100m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m second mutable borrow occurs here",
      " \u001b[38;5;246m6 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68mr\u001b[0m\u001b[38;5;68m1\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                    \u001b[38;5;68m─\u001b[0m\u001b[38;5;68m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                     \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m first borrow later used here",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "    let r1 = &mut s;\n",
    "    r1.push_str(\", world\");\n",
    "    s.push_str(\" again\");\n",
    "    println!(\"{}\", r1);\n",
    "    println!(\"{}\", s);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea4124aa-9fcd-4376-b81b-802f87606582",
   "metadata": {},
   "source": [
    "- Cannot have two or more mutable references\n",
    "- Avoids *race conditions* in concurrent programs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7539e765-5865-4b4d-ad61-08343f87d001",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot borrow `s` as mutable more than once at a time",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0499] Error:\u001b[0m cannot borrow `s` as mutable more than once at a time",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_25:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m4 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m1\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54m&\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54mu\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m first mutable borrow occurs here",
      " \u001b[38;5;246m5 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;100m&\u001b[0m\u001b[38;5;100mm\u001b[0m\u001b[38;5;100mu\u001b[0m\u001b[38;5;100mt\u001b[0m\u001b[38;5;100m \u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m┬\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m second mutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m7 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68mr\u001b[0m\u001b[38;5;68m1\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                        \u001b[38;5;68m─\u001b[0m\u001b[38;5;68m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                         \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m first borrow later used here",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "\n",
    "    let r1 = &mut s;\n",
    "    let r2 = &mut s;\n",
    "\n",
    "    println!(\"{}, {}\", r1, r2);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c623ff7-45d4-4360-a6f2-b74f3afc8715",
   "metadata": {},
   "source": [
    "- Here the second mutable reference is created after the first one goes out of scope, so this is fine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "034005ea-6add-4a90-84b0-11d08e4ac9b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "\n",
    "    {\n",
    "        let r1 = &mut s;\n",
    "    } // r1 goes out of scope here, so we can make a new reference with no problems.\n",
    "\n",
    "    let r2 = &mut s;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61f291d6-da6e-4230-a528-f5f49eb0b1af",
   "metadata": {},
   "source": [
    "- Cannot mix immutable and mutable references\n",
    "- Again to avoid race conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8739e86d-3c72-43b7-87c9-6c55ba9d58bd",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot borrow `s` as mutable because it is also borrowed as immutable",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0502] Error:\u001b[0m cannot borrow `s` as mutable because it is also borrowed as immutable",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_27:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m4 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m1\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;100m&\u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;249m;\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mb\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mm\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;100m─\u001b[0m\u001b[38;5;100m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m               \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m immutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m6 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m3\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54m&\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54mu\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;249m;\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mB\u001b[0m\u001b[38;5;249mI\u001b[0m\u001b[38;5;249mG\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mP\u001b[0m\u001b[38;5;249mR\u001b[0m\u001b[38;5;249mO\u001b[0m\u001b[38;5;249mB\u001b[0m\u001b[38;5;249mL\u001b[0m\u001b[38;5;249mE\u001b[0m\u001b[38;5;249mM\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m              \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m mutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m8 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68mr\u001b[0m\u001b[38;5;68m1\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m2\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m3\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                                \u001b[38;5;68m─\u001b[0m\u001b[38;5;68m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                                 \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m immutable borrow later used here",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "\n",
    "    let r1 = &s; // no problem\n",
    "    let r2 = &s; // no problem\n",
    "    let r3 = &mut s; // BIG PROBLEM\n",
    "\n",
    "    println!(\"{}, {}, and {}\", r1, r2, r3);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0961ad23-021b-4f17-bfad-4944f9e91086",
   "metadata": {},
   "source": [
    "- Here the last use of `r1` and `r2` occurs before `r3` is declared\n",
    "- Rust does sophisticated static analysis to determine this at compile time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9b25ef9b-5227-49b2-abe5-f47a7db6c333",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello and hello\n",
      "hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\n",
    "    let mut s = String::from(\"hello\");\n",
    "\n",
    "    let r1 = &s; // no problem\n",
    "    let r2 = &s; // no problem\n",
    "    println!(\"{} and {}\", r1, r2);\n",
    "    // variables r1 and r2 will not be used after this point\n",
    "\n",
    "    let r3 = &mut s; // no problem\n",
    "    println!(\"{}\", r3);\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f65f4802-b42e-462a-86ec-88549af1b5ba",
   "metadata": {},
   "source": [
    "- Unlike `gives_ownership` earlier, here `dangle` returns a reference\n",
    "- Potential problem --- when `dangle` exits, `s` goes out of scope and `reference_to_nothing` becomes a *dangling pointer*, pointing to nothing\n",
    "- Rust catches this as a compile-time error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f17c0262-0efd-462e-9dd2-7c87c7bba6d6",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "missing lifetime specifier",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0106] Error:\u001b[0m missing lifetime specifier",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_29:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m5 │\u001b[0m \u001b[38;5;249mf\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m-\u001b[0m\u001b[38;5;249m>\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54m&\u001b[0m\u001b[38;5;249mS\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mg\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;54m┬\u001b[0m\u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m expected named lifetime parameter",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;68m│\u001b[0m\u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m help: instead, you are more likely to want to return an owned value: ``",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m  │\u001b[0m                 \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`: `'static `",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "fn main() {\n",
    "    let reference_to_nothing = dangle();\n",
    "}\n",
    "\n",
    "fn dangle() -> &String {\n",
    "    let s = String::from(\"hello\");\n",
    "    &s\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e208998-0567-4a02-8ac7-da0c2167597a",
   "metadata": {},
   "source": [
    "### Slices"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "891e5865-8e26-46ca-b254-5b617b6747cc",
   "metadata": {},
   "source": [
    "- A function to compute the length of the first word in a string\n",
    "- `bytes.iter()` iterates through `bytes`, `enumerate()` returns a pair (index,reference to value), which is deomposed through pattern matching into `(i, &item)`\n",
    "- `b' '` specifies a byte constant for the space character"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f3cb95eb-be04-484c-b36c-468c507f4388",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn first_word(s: &String) -> usize {\n",
    "    let bytes = s.as_bytes();\n",
    "\n",
    "    for (i, &item) in bytes.iter().enumerate() {\n",
    "        if item == b' ' {\n",
    "            return i;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    s.len()\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e206d47d-deca-42db-9bf3-3ef2254c2723",
   "metadata": {},
   "source": [
    "- In this function, Rust cannot recognize that the return value is an index into the string\n",
    "- If we clear the string, the index is no longer valid, but cannot be flagged by compiler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f2a3140d-4401-4fa5-b045-bdf11ee7ef4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn main() {\n",
    "    let mut s = String::from(\"hello world\");\n",
    "\n",
    "    let word = first_word(&s); // word will get the value 5\n",
    "\n",
    "    s.clear(); // this empties the String, making it equal to \"\"\n",
    "\n",
    "    // word still has the value 5 here, but there's no more string that\n",
    "    // we could meaningfully use the value 5 with. word is now totally invalid!\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5c7dddc-39c1-413f-aac0-c39515c409bc",
   "metadata": {},
   "source": [
    "- A string slice is written similar to a slice in Python\n",
    "- Gives a reference to a substring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3510105b-5e60-4883-987e-2828b31cc9b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{ \n",
    "    let s = String::from(\"hello world\");\n",
    "\n",
    "    let hello = &s[0..5];\n",
    "    let world = &s[6..11];\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d98607f8-1640-448f-af5c-6ec8d93e7bb0",
   "metadata": {},
   "source": [
    "- Rewrite `first_word` to return slice corresponding to first word\n",
    "- Will examine distinction between `&String` and `&str` later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bea672c1-e4b0-4bf4-9479-bfdc362ad7a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "fn first_word(s: &String) -> &str {\n",
    "    let bytes = s.as_bytes();\n",
    "\n",
    "    for (i, &item) in bytes.iter().enumerate() {\n",
    "        if item == b' ' {\n",
    "            return &s[0..i];\n",
    "        }\n",
    "    }\n",
    "\n",
    "    &s[..]\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4d176c9-5403-4d50-9057-fb2ba4b75921",
   "metadata": {},
   "source": [
    "- Now, if we try to clear the \"parent\" string while holding a reference to a substring, it is a compile error\n",
    "- Another example of combining immutable and mutable references --- the call `s.clear()` implicitly passes a mutable reference to `s` to `clear()`, while `word` currently holds an immutable reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce25fd50-2e2f-417e-8a0e-6e26133003c2",
   "metadata": {},
   "outputs": [
    {
     "ename": "Error",
     "evalue": "cannot borrow `s` as mutable because it is also borrowed as immutable",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0502] Error:\u001b[0m cannot borrow `s` as mutable because it is also borrowed as immutable",
      "   \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_34:1:1\u001b[38;5;246m]\u001b[0m",
      "   \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m4 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mw\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mf\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m_\u001b[0m\u001b[38;5;249mw\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;100m&\u001b[0m\u001b[38;5;100ms\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                           \u001b[38;5;100m─\u001b[0m\u001b[38;5;100m┬\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                            \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m\u001b[38;5;100m─\u001b[0m immutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m6 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54m.\u001b[0m\u001b[38;5;54mc\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54ma\u001b[0m\u001b[38;5;54mr\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;249m;\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m/\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249m!\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m     \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m         \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m mutable borrow occurs here",
      " \u001b[38;5;240m  │\u001b[0m ",
      " \u001b[38;5;246m8 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249m!\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mh\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mf\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mw\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249mr\u001b[0m\u001b[38;5;249md\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249ms\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m\u001b[38;5;249m}\u001b[0m\u001b[38;5;249m\"\u001b[0m\u001b[38;5;249m,\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;68mw\u001b[0m\u001b[38;5;68mo\u001b[0m\u001b[38;5;68mr\u001b[0m\u001b[38;5;68md\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m  │\u001b[0m                                       \u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m┬\u001b[0m\u001b[38;5;68m─\u001b[0m  ",
      " \u001b[38;5;240m  │\u001b[0m                                         \u001b[38;5;68m╰\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m\u001b[38;5;68m─\u001b[0m immutable borrow later used here",
      "\u001b[38;5;246m───╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "fn main() {\n",
    "    let mut s = String::from(\"hello world\");\n",
    "\n",
    "    let word = first_word(&s);\n",
    "\n",
    "    s.clear(); // error!\n",
    "\n",
    "    println!(\"the first word is: {}\", word);\n",
    "}\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": 5
}
