[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-learn-rust-ep8-borrowing-and-references-all--*":3,"academy-blog-translations-k2bmf1p6o6kvw2q":73},{"data":4,"page":72,"perPage":72,"totalItems":72,"totalPages":72},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":66,"keywords":67,"locale":42,"published_at":68,"scheduled_at":13,"school_blog":64,"short_description":69,"slug":70,"status":62,"title":71,"updated":68,"updated_by":13,"views":65},"Code examples and Borrow Checker rules.","sclblg987654321","school_blog_translations","\u003Cp>Welcome back, fellow developers, to \u003Ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fblogs\u002Fcategories\u002FRust%20The%20Series\">\u003Cstrong>Rust The Series!\u003C\u002Fstrong>\u003C\u002Fa> In EP.7, we witnessed the strict (but safe) rules of Ownership. We saw how moving a value causes the original variable to \"die\" instantly. You might be feeling a bit frustrated and asking yourself:\u003C\u002Fp>\u003Cblockquote>\u003Cp>\u003Cem>\"Do I really have to call \u003Ccode>.clone()\u003C\u002Fcode> and waste memory every time I want to pass data to a function? Or do I have to return the value back every single time just to give ownership back to the original variable?\"\u003C\u002Fem>\u003C\u002Fp>\u003C\u002Fblockquote>\u003Cp>The answer is \u003Cstrong>\"Absolutely not!\"\u003C\u002Fstrong> Rust provides a sophisticated system called \u003Cstrong>Borrowing\u003C\u002Fstrong>, powered by \u003Cstrong>References (&amp;)\u003C\u002Fstrong>, designed to make our lives much easier.\u003C\u002Fp>\u003Ch2>1. Immutable References (Read-Only Borrowing)\u003C\u002Fh2>\u003Cp>Borrowing in Rust is just like \u003Cstrong>\"borrowing a book from a library\"\u003C\u002Fstrong>:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>You can read it (Access the data).\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>You do \u003Cstrong>not\u003C\u002Fstrong> own it (No Ownership).\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>Most importantly: \u003Cstrong>You are strictly forbidden from writing or modifying the content!\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>In Rust, we use the \u003Cstrong>\u003Ccode>&amp;\u003C\u002Fcode>\u003C\u002Fstrong> symbol before a variable name to tell the compiler: \u003Cem>\"This is a reference. I’m just letting the function borrow it for a look; I’m not giving away the ownership.\"\u003C\u002Fem>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let s1 = String::from(\"Superdev\");\n\n    \u002F\u002F Passing &amp;s1 means we are \"lending\" the data\n    let len = calculate_length(&amp;s1); \n\n    \u002F\u002F ✅ s1 is still valid! Ownership remains here in the main function.\n    println!(\"The length of '{}' is {}.\", s1, len); \n}\n\nfn calculate_length(s: &amp;String) -&gt; usize { \u002F\u002F Parameter is a Reference (&amp;)\n    s.len()\n} \u002F\u002F Here, s goes out of scope. \n  \u002F\u002F However, the actual data is NOT deleted because s was only a borrower, not the owner.\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cblockquote>\u003Cp>\u003Cstrong>💡 Pro Tip:\u003C\u002Fstrong> Notice that when calling \u003Ccode>calculate_length(&amp;s1)\u003C\u002Fcode>, we use the \u003Ccode>&amp;\u003C\u002Fcode> symbol, and the function signature \u003Ccode>(s: &amp;String)\u003C\u002Fcode> also requires the \u003Ccode>&amp;\u003C\u002Fcode>. This acts as a \"contract\" between the sender and the receiver, both agreeing that \u003Cstrong>\"this is just a loan.\"\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Fblockquote>\u003Ch2>2. Modifying Data: Mutable References (&amp;mut)\u003C\u002Fh2>\u003Cp>If an immutable reference is for \"reading,\" a \u003Cstrong>Mutable Reference\u003C\u002Fstrong> is for \u003Cstrong>\"borrowing to edit.\"\u003C\u002Fstrong> Imagine borrowing a book from a friend and asking, \u003Cem>\"Hey, do you mind if I take some notes inside?\"\u003C\u002Fem> Your friend can only say yes if they (the owner) decided the book was \"writable\" (mutable) from the very beginning.\u003C\u002Fp>\u003Cp>In Rust, we use the \u003Cstrong>\u003Ccode>&amp;mut\u003C\u002Fcode>\u003C\u002Fstrong> symbol for this. However, there is one crucial condition: \u003Cstrong>The original variable must be declared as \u003Ccode>mut\u003C\u002Fcode> first.\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    \u002F\u002F 1. The original variable must be 'mut' to allow others to modify it.\n    let mut s = String::from(\"Hello\");\n\n    \u002F\u002F 2. When passing it, you must use '&amp;mut' to confirm you are \"lending it for modification.\"\n    change(&amp;mut s); \n\n    println!(\"{}\", s); \u002F\u002F Result: \"Hello, Superdev\"\n}\n\nfn change(some_string: &amp;mut String) { \u002F\u002F 3. The receiver must specify '&amp;mut String'\n    some_string.push_str(\", Superdev\");\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cblockquote>\u003Cp>\u003Cstrong>⚠️ Warning:\u003C\u002Fstrong> While you can borrow to edit, Rust is stricter than your average friend. The compiler enforces a major rule: \u003Cstrong>\"You can only have one mutable reference (&amp;mut) to a piece of data at a time.\"\u003C\u002Fstrong> No one else can borrow it (even just to read) while someone is editing it. We’ll dive into the \"why\" behind this in the next section.\u003C\u002Fp>\u003C\u002Fblockquote>\u003Ch2>3. The Rules of Borrowing\u003C\u002Fh2>\u003Cp>To ensure maximum safety and prevent every programmer’s nightmare—\u003Cstrong>Data Races\u003C\u002Fstrong> (where data is modified simultaneously, causing crashes or corruption)—Rust enforces two ironclad rules that the compiler checks every time:\u003C\u002Fp>\u003Ch3>Rule 1: Many Readers OR One Writer\u003C\u002Fh3>\u003Cp>At any given time, you can have \u003Cstrong>only one\u003C\u002Fstrong> of these two situations:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Multiple \"Readers\" (\u003Ccode>&amp;\u003C\u002Fcode>):\u003C\u002Fstrong> You can have as many immutable references as you want. It’s like reading a Wikipedia page; everyone can read it simultaneously because the content isn't changing.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Exactly ONE \"Writer\" (\u003Ccode>&amp;mut\u003C\u002Fcode>):\u003C\u002Fstrong> You can have only one mutable reference. While this person is \"writing\" or \"editing,\" \u003Cstrong>no one else\u003C\u002Fstrong> is allowed to even look at the data.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>Rule 2: References Must Always Be Valid (No Dangling References)\u003C\u002Fh3>\u003Cp>You cannot borrow something from someone who no longer exists. A reference must never outlive the \u003Cstrong>actual owner\u003C\u002Fstrong> of the data in memory.\u003C\u002Fp>\u003Cp>\u003Cstrong>Let’s look at an example that will make the compiler scream at us:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut s = String::from(\"hello\");\n\n    let r1 = &amp;s;     \u002F\u002F ✅ Borrowing to read (OK)\n    let r2 = &amp;s;     \u002F\u002F ✅ Borrowing to read (OK)\n    \n    \u002F\u002F ❌ ERROR! The compiler will stop you right here.\n    let r3 = &amp;mut s; \n\n    println!(\"{}, {}, and {}\", r1, r2, r3);\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>Why does this fail?\u003C\u002Fh3>\u003Cp>Imagine \u003Ccode>r1\u003C\u002Fcode> and \u003Ccode>r2\u003C\u002Fcode> are focused on reading a piece of paper. Suddenly, \u003Ccode>r3\u003C\u002Fcode> walks up and starts crossing out words and writing new ones right in front of them! The information \u003Ccode>r1\u003C\u002Fcode> and \u003Ccode>r2\u003C\u002Fcode> were just reading is now invalid and inconsistent.\u003C\u002Fp>\u003Cp>Rust stops this chaos at the source: \u003Cstrong>\"If someone is reading, no one can edit\"\u003C\u002Fstrong> and \u003Cstrong>\"If someone is editing, no one else can read.\"\u003C\u002Fstrong> This guarantees that the data in your hand is 100% accurate and safe, always.\u003C\u002Fp>\u003Ch3>4. Why Suffer Through These Rules? (The Benefits)\u003C\u002Fh3>\u003Cp>You might be thinking, \"Man, that’s a lot of rules!\" But trust me, once you master them, you’ll be writing world-class code that reaps these massive rewards:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>🚀 \u003Cstrong>Extreme Performance:\u003C\u002Fstrong> We no longer waste time or resources on heavy \u003Ccode>.clone()\u003C\u002Fcode> operations or copying large data on the \u003Cstrong>Heap\u003C\u002Fstrong> every time we pass it to a function. Using a Reference is simply passing a small \"address\" (Pointer), which is incredibly fast.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🛡️ \u003Cstrong>Top-Tier Safety:\u003C\u002Fstrong> Rust’s \u003Cstrong>Borrow Checker\u003C\u002Fstrong> audits your code’s safety while you write it. You don't have to cross your fingers and hope the program doesn't crash at \u003Cstrong>Runtime\u003C\u002Fstrong>. You can rest easy knowing that no one is \"editing data behind your back.\"\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>💎 \u003Cstrong>Clean Code &amp; Better DX (Developer Experience):\u003C\u002Fstrong> Your code becomes more readable and natural. You won’t need to write awkward functions that constantly return the original value just to maintain Ownership.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch3>Summary\u003C\u002Fh3>\u003Cp>The Borrowing system is the heart of what makes Rust both practical and incredibly powerful. While the rules might feel a bit \"picky\" at first, once you get into the rhythm of identifying \"Who is the owner?\" and \"Who is just borrowing?\", you'll find yourself writing lightning-fast code. Your programs will be immune to memory corruption and, most importantly, \"completely free of memory leaks\"—all without ever needing a Garbage Collector!\u003C\u002Fp>\u003Cp>📌 \u003Cstrong>In the next episode (EP.9):\u003C\u002Fstrong> We’ll keep the momentum going with Slices. You'll learn how to \"borrow\" only a specific portion of data (for example, how to grab just the first word of a long sentence). This technique will elevate your ability to manage Arrays and Strings to a professional level... See you there! 🦀✨\u003C\u002Fp>\u003Cp>🎯 \u003Cstrong>Follow Superdev Academy for top-tier Dev knowledge:\u003C\u002Fstrong> Don't miss out on deep-dive technical articles and the latest updates from the \u003Cstrong>Superdev Academy\u003C\u002Fstrong> team. Follow us on all our channels here:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy Thailand\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">\u003Cstrong>Superdev Academy Channel\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"http:\u002F\u002Fsuperdevacademy.com\">\u003Cstrong>superdevacademy.com\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","16kid52yn542_5ki54tgvw3.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fx11me5c0pg69cr8\u002F16kid52yn542_5ki54tgvw3.png","2026-04-27 03:05:03.384Z","",{"keywords":15,"locale":36,"school_blog":46},[16,22,26,31],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":19,"updated_by":13},"sclkey987654321","school_keywords","2026-04-27 03:00:57.235Z","e48382bzq7x36ct","Rust Borrowing",{"collectionId":17,"collectionName":18,"created":23,"created_by":13,"id":24,"name":25,"updated":23,"updated_by":13},"2026-04-27 03:01:05.258Z","qtnolt9tk6qjebz","References",{"collectionId":17,"collectionName":18,"created":27,"created_by":13,"id":28,"name":29,"updated":30,"updated_by":13},"2026-03-04 08:20:06.235Z","254619mvnk1hr7y","Rust","2026-04-10 16:07:25.292Z",{"collectionId":17,"collectionName":18,"created":32,"created_by":13,"id":33,"name":34,"updated":35,"updated_by":13},"2026-03-23 03:26:43.307Z","pf009wnev852vvt","Rust tutorial","2026-04-10 16:14:45.235Z",{"code":37,"collectionId":38,"collectionName":39,"created":40,"flag":41,"id":42,"is_default":43,"label":44,"updated":45},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":47,"collectionId":48,"collectionName":49,"expand":50,"id":64,"views":65},"mfqe235iv6x1or8","pbc_2105096300","school_blogs",{"category":51},{"blogIds":52,"collectionId":53,"collectionName":54,"created":55,"created_by":13,"id":47,"image":56,"image_alt":13,"image_path":57,"label":58,"name":59,"priority":60,"publish_at":61,"scheduled_at":13,"status":62,"updated":63,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-16 04:40:07.104Z","47vt5s1gglj_tz0b90vnge.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fmfqe235iv6x1or8\u002F47vt5s1gglj_tz0b90vnge.png",{"en":59,"th":59},"Rust The Series",2,"2026-03-16 04:40:09.199Z","published","2026-04-25 02:32:15.619Z","k2bmf1p6o6kvw2q",185,"x11me5c0pg69cr8",[20,24,28,33],"2026-04-27 03:20:34.558Z","A complete guide to Rust Borrowing and References. Learn how to borrow data using & and &mut, understand the Borrow Checker rules, and prevent data races in your Rust applications.","learn-rust-ep8-borrowing-and-references","Understanding Borrowing and References | Rust The Series EP.8",1,{"th":70,"en":70}]