[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-rust-ownership-memory-management-ep7-all--*":3,"academy-blog-translations-ce57lvevpczp1sj":83},{"data":4,"page":82,"perPage":82,"totalItems":82,"totalPages":82},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":76,"keywords":77,"locale":52,"published_at":13,"scheduled_at":13,"school_blog":74,"short_description":78,"slug":79,"status":72,"title":80,"updated":81,"updated_by":13,"views":75},"Rust The Series EP.7 cover image explaining Ownership and memory management","sclblg987654321","school_blog_translations","\u003Cp>Welcome back to \u003Cstrong>Rust The Series\u003C\u002Fstrong>! In EP.6, we learned how to steer our programs using Control Flow. Today, we're stepping into the \u003Cstrong>\"absolute core\"\u003C\u002Fstrong> of the Rust programming language.\u003C\u002Fp>\u003Cp>If you ask what makes Rust stand out and completely different from popular languages like C++, Java, Go, or Python? The short answer is a single word: \u003Cstrong>Ownership\u003C\u002Fstrong>!\u003C\u002Fp>\u003Cp>This system is the ultimate secret weapon that allows Rust to manage memory with the high-performance efficiency of C\u002FC++. But what's even cooler is that it guarantees you \u003Cstrong>\"won't encounter bugs\"\u003C\u002Fstrong> like Memory Leaks or memory mismanagement that crashes your program. And most importantly... it does all this \u003Cstrong>without needing a Garbage Collector (GC)\u003C\u002Fstrong> to slow your program down!\u003C\u002Fp>\u003Cp>How does this system, which sounds almost like magic, work behind the scenes? Without further ado, let's dive deep into the \u003Cstrong>3 Ironclad Rules of Ownership\u003C\u002Fstrong>!\u003C\u002Fp>\u003Ch2>1. The 3 Ironclad Rules of Ownership\u003C\u002Fh2>\u003Cp>In the world of Rust, for the program to manage memory itself right from compile time, you need to memorize these 3 rules:\u003C\u002Fp>\u003Col>\u003Cli>\u003Cp>\u003Cstrong>Every piece of data\u003C\u002Fstrong> in Rust must have a variable that is its \u003Cstrong>\"Owner\"\u003C\u002Fstrong>.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>At any given time, a piece of data can have \u003Cstrong>\"only one owner\"\u003C\u002Fstrong>.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>When the owner variable goes \u003Cstrong>\"out of scope\"\u003C\u002Fstrong>, the data will be immediately cleared and dropped from memory!\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Fol>\u003Ch2>2. Scope and Drop\u003C\u002Fh2>\u003Cp>Let's look at a simple example of what a Scope is. In Rust, \u003Cstrong>curly braces \u003Ccode>{}\u003C\u002Fcode> define the lifespan of a variable\u003C\u002Fstrong>. To clearly visualize memory allocation and deallocation, we will use \u003Ccode>String::from\u003C\u002Fcode>, which creates data on the Heap Memory:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    {                      \u002F\u002F s is not valid here, it’s not yet declared\n        let s = String::from(\"hello\");   \u002F\u002F s is valid from this point forward, allocating space on the Heap\n        println!(\"{}\", s);\n    }                      \u002F\u002F &lt;--- Scope ends! The variable s is destroyed (calling the Drop function), instantly returning memory to the system\n    \n    \u002F\u002F println!(\"{}\", s);  \u002F\u002F ❌ If you force a call to s here, the compiler will yell at you immediately because s is already dead!\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>Notice that the moment the closing brace \u003Ccode>}\u003C\u002Fcode> is reached, Rust knows its job and automatically returns the memory for us without us ever needing to manually command it to Free Memory. This is exactly what makes it so awesome!\u003C\u002Fp>\u003Ch2>3. Transferring Ownership (Move)\u003C\u002Fh2>\u003Cp>Rule #2 clearly states that there can be \u003Cstrong>\"only one owner\"\u003C\u002Fstrong>. Let's experience the first real pain of anyone starting to write Rust. Suppose we use the \u003Ccode>String\u003C\u002Fcode> data type (which stores its actual data on the Heap Memory):\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let s1 = String::from(\"Superdev\");\n    let s2 = s1; \u002F\u002F Ownership is instantly \"Moved\" from s1 to s2\n\n    \u002F\u002F println!(\"The value of s1 is {}\", s1); \u002F\u002F ❌ This line will Error immediately!\n    println!(\"The value of s2 is {}\", s2);    \u002F\u002F ✅ Works perfectly fine\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003Cstrong>What just happened?\u003C\u002Fstrong> In other languages, doing this creates a reference where two variables point to the exact same block of data. But for Rust, when you declare \u003Ccode>let s2 = s1;\u003C\u002Fcode>, it's a \u003Cstrong>\"takeover\"\u003C\u002Fstrong>!\u003C\u002Fp>\u003Cp>Ownership immediately falls to \u003Ccode>s2\u003C\u002Fcode>, and the variable \u003Ccode>s1\u003C\u002Fcode> is considered an \u003Cstrong>\"invalidated variable\"\u003C\u002Fstrong>, meaning it can no longer be used.\u003C\u002Fp>\u003Cp>\u003Cstrong>Why does Rust do this?\u003C\u002Fstrong> To prevent a critical bug known as a \u003Cstrong>Double Free Error\u003C\u002Fstrong>. Imagine this: if \u003Ccode>s1\u003C\u002Fcode> and \u003Ccode>s2\u003C\u002Fcode> pointed to the same memory location, when both variables go out of scope, they would both try to free the same block of memory twice. This would instantly crash the program! Rust nips this problem in the bud by revoking \u003Ccode>s1\u003C\u002Fcode>'s rights completely.\u003C\u002Fp>\u003Ch2>4. What if you really want to copy? (Clone)\u003C\u002Fh2>\u003Cp>If there is a necessary reason we don't want to transfer ownership (Move), but instead want to \u003Cstrong>\"copy\"\u003C\u002Fstrong> the data into a completely separate duplicate (Deep Copy), we can use the \u003Ccode>.clone()\u003C\u002Fcode> method to help out:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let s1 = String::from(\"Superdev\");\n    let s2 = s1.clone(); \u002F\u002F Creates a brand new set of data in another memory block for s2 to own\n\n    println!(\"s1 = {}, s2 = {}\", s1, s2); \u002F\u002F ✅ Both work fine because they have different owners and use completely different memory blocks\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>🔥 \u003Cstrong>Rust Trick: Why don't numbers lose ownership?\u003C\u002Fstrong> If you try doing the exact same thing with numbers (Integers), you might be surprised that it compiles and runs just fine!\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let x = 5;\n    let y = x;\n    println!(\"x = {}, y = {}\", x, y); \u002F\u002F ✅ It works! x doesn't lose its ownership\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003Cstrong>Why does this happen?\u003C\u002Fstrong> The reason is that basic data types with a fixed size (Primitive types like Integer, Float, Boolean, Char) are stored in the \u003Cstrong>Stack Memory\u003C\u002Fstrong>. Copying data on the Stack is incredibly fast and consumes very few resources (it's very cheap).\u003C\u002Fp>\u003Cp>Therefore, Rust automatically copies the data for us without needing to perform a Move like it does with a \u003Ccode>String\u003C\u002Fcode>. (This underlying behavior is known as the \u003Cstrong>Copy Trait\u003C\u002Fstrong>, which we will dive deeper into in later EPs.)\u003C\u002Fp>\u003Cp>\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>Conclusion\u003C\u002Fh2>\u003Cp>Ownership is a concept designed to force us to write safe code right from the start (Compile time). At first, you might feel like, \"Why is the compiler so aggressive? I just moved a value and it throws an Error!\" But trust me, if you can get past Rust's compiler, your program will be rock-solid, lightning-fast, and practically bug-free when deployed to Production!\u003C\u002Fp>\u003Cp>📌 \u003Cstrong>In the next EP (EP.8):\u003C\u002Fstrong> If we had to \u003Ccode>.clone()\u003C\u002Fcode> every single time we passed a value into a function, our memory would fill up in no time! We'll look at how to solve this problem by \u003Cstrong>\"Borrowing\"\u003C\u002Fstrong> (Borrowing &amp; References), allowing us to use data without wrestling for ownership. Get ready, and see you in the next article! 🦀\u003C\u002Fp>\u003Ch3>🎯 Follow for top-tier Dev knowledge at:\u003C\u002Fh3>\u003Cp>Don't want to miss our in-depth technical articles and new updates? Follow \u003Cstrong>Superdev Academy\u003C\u002Fstrong> across 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\">Superdev Academy Thailand\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\">Superdev Academy Channel\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\">@superdevacademy\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\">@superdevacademy\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🌐 \u003Cstrong>Website:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"http:\u002F\u002Fsuperdevacademy.com\">superdevacademy.com\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","14eoqfi7icgx_9fc0p0hswx.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fn3ass0bdq6jh1fa\u002F14eoqfi7icgx_9fc0p0hswx.png","2026-04-17 07:45:52.314Z","",{"keywords":15,"locale":46,"school_blog":56},[16,23,28,32,36,41],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:06.235Z","254619mvnk1hr7y","Rust","2026-04-10 16:07:25.292Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-16 05:11:00.000Z","2ypk85up2gune0d","Ownership Rust","2026-04-10 16:14:42.143Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":29,"updated_by":13},"2026-04-17 07:41:10.212Z","mxgbsh34gakme8i","Garbage Collector",{"collectionId":17,"collectionName":18,"created":33,"created_by":13,"id":34,"name":35,"updated":33,"updated_by":13},"2026-04-17 07:41:18.480Z","gye5gzd7bmbzwac","Memory Leak",{"collectionId":17,"collectionName":18,"created":37,"created_by":13,"id":38,"name":39,"updated":40,"updated_by":13},"2026-03-04 08:44:11.146Z","gms2qr4xg6qv65e","Superdev Academy","2026-04-10 16:12:41.207Z",{"collectionId":17,"collectionName":18,"created":42,"created_by":13,"id":43,"name":44,"updated":45,"updated_by":13},"2026-03-04 08:20:13.847Z","wqqb4is6ir6hsig","Rust Language","2026-04-10 16:07:26.046Z",{"code":47,"collectionId":48,"collectionName":49,"created":50,"flag":51,"id":52,"is_default":53,"label":54,"updated":55},"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":57,"collectionId":58,"collectionName":59,"expand":60,"id":74,"views":75},"mfqe235iv6x1or8","pbc_2105096300","school_blogs",{"category":61},{"blogIds":62,"collectionId":63,"collectionName":64,"created":65,"created_by":13,"id":57,"image":66,"image_alt":13,"image_path":67,"label":68,"name":69,"priority":70,"publish_at":71,"scheduled_at":13,"status":72,"updated":73,"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":69,"th":69},"Rust The Series",2,"2026-03-16 04:40:09.199Z","published","2026-04-25 02:32:15.619Z","ce57lvevpczp1sj",118,"n3ass0bdq6jh1fa",[20,25,30,34,38,43],"Discover the core of Rust programming. Learn the 3 rules of Ownership that make your code blazing fast and memory-safe without needing a Garbage Collector.","rust-ownership-memory-management-ep7","Rust Ownership Explained: How to Manage Memory Without a Garbage Collector | Rust The Series EP.7","2026-04-27 03:11:41.611Z",1,{"th":79,"en":79}]