<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[No Stack to Full Stack]]></title><description><![CDATA[Real notes from a developer who learns in public. Every post starts as a messy idea and becomes something worth reading clear, accurate, and written to actually]]></description><link>https://blog.abisheka.in</link><generator>RSS for Node</generator><lastBuildDate>Wed, 13 May 2026 08:16:22 GMT</lastBuildDate><atom:link href="https://blog.abisheka.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Rust Structs: From Scattered Variables to Organized, Method-Powered Types]]></title><description><![CDATA[I was staring at a function signature that looked like this:
fn area(width: u32, height: u32) -> u32 {
    width * height
}

And something felt off. Not wrong — it works. But it smells. Width and heig]]></description><link>https://blog.abisheka.in/rust-structs-from-scattered-variables-to-organized-method-powered-types</link><guid isPermaLink="true">https://blog.abisheka.in/rust-structs-from-scattered-variables-to-organized-method-powered-types</guid><category><![CDATA[rust lang]]></category><category><![CDATA[Rust]]></category><category><![CDATA[Structs in Rust]]></category><category><![CDATA[structs]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Abishek A]]></dc:creator><pubDate>Wed, 13 May 2026 06:13:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/66827a195bf5be060a0dd888/100d9156-6911-443b-b48e-82b7182111c3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I was staring at a function signature that looked like this:</p>
<pre><code class="language-rust">fn area(width: u32, height: u32) -&gt; u32 {
    width * height
}
</code></pre>
<p>And something felt off. Not wrong — it works. But it <em>smells</em>. Width and height clearly belong together. They're describing the same thing. Why are they just floating around as separate arguments like two strangers at a party who definitely know each other?</p>
<p>That's when structs clicked for me.</p>
<hr />
<h2>What Is a Struct, Really?</h2>
<p>Think of a struct like a form. A user registration form has fields: name, email, whether the account is active. Those fields don't make sense in isolation — they belong together under the concept of "a user." A struct is how you express that grouping in Rust.</p>
<p>Where tuples let you group things by position (<code>(30, 50)</code> — wait, which one is width?), structs let you group things by <em>name</em>. That's the core win.</p>
<p>Here's the User struct I practiced with:</p>
<pre><code class="language-rust">struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}
</code></pre>
<p>Different types, named fields, grouped under one concept. Instantly more readable than a tuple of <code>(String, String, u64, bool)</code>.</p>
<hr />
<h2>Creating and Using Instances</h2>
<p>Once you have a struct defined, you create <em>instances</em> of it. Each instance fills in the actual values for those fields:</p>
<pre><code class="language-rust">let mut user1 = User {
    email: String::from("padfoot@gmail.com"),
    username: String::from("padfoot"),
    sign_in_count: 1,
    active: true,
};
</code></pre>
<p>Fields don't have to go in declaration order — Rust doesn't care. Access them with dot notation:</p>
<pre><code class="language-rust">let name = user1.username;
</code></pre>
<p>And if you marked the instance <code>mut</code>, you can update fields the same way:</p>
<pre><code class="language-rust">user1.username = String::from("0x12md10");
</code></pre>
<p>One important thing I noticed: <strong>mutability is all-or-nothing on the whole struct</strong>. You can't say "only this field is mutable." Either the whole instance is <code>mut</code> or none of it is.</p>
<img src="https://media.tenor.com/pVSeBDWeMSsAAAAj/it%27s-all-or-nothing-mate-colin-lawson.gif" alt="All or Nothing" style="display:block;margin:0 auto" />
 
<hr />
<h2>Build Functions and Field Init Shorthand</h2>
<p>Often you'll want a constructor-style function to build instances. I wrote one called <code>build_user</code>:</p>
<pre><code class="language-rust">fn build_user(email: String, username: String) -&gt; User {
    User {
        email,
        username,
        active: true,
        sign_in_count: 1,
    }
}
</code></pre>
<p>Notice anything about <code>email</code> and <code>username</code> in the struct literal? No <code>email: email</code> — just <code>email</code>. When a function parameter has the same name as the struct field, Rust lets you use the <strong>field init shorthand</strong> and skip the redundant assignment. It's a small thing but it removes a lot of visual noise when you have many fields.</p>
<hr />
<h2>Struct Update Syntax</h2>
<p>Another handy feature: creating a new instance from an existing one, only overriding what's different.</p>
<pre><code class="language-rust">let user2 = build_user(String::from("harry@gmail.com"), String::from("Harry"));
 
let user3 = User {
    email: String::from("BruceW@gmail.com"),
    username: String::from("Bruce Wayne"),
    ..user2
};
</code></pre>
<p>The <code>..user2</code> says: "for any fields I haven't specified, copy them from <code>user2</code>." So <code>user3</code> gets <code>active</code> and <code>sign_in_count</code> from <code>user2</code>. Clean.</p>
<hr />
<h2>Tuple Structs: Named Tuples</h2>
<p>Sometimes you want a tuple to have a type name — so you can distinguish a <code>Color</code> from a <code>Point</code> even when they have the same shape.</p>
<pre><code class="language-rust">struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
</code></pre>
<p>Both have three <code>i32</code>s. But they're different types. A function expecting a <code>Point</code> won't accept a <code>Color</code>. This is useful when type safety matters more than field names.</p>
<img src="https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExbGtuNWd3Y2ZidXNncWEwMXRkYmd5OGR2d3gwMGJhN3U5YmRodG9mMCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l36kU80xPf0ojG0Erg/giphy.gif" alt="they look the same but they're not meme" style="display:block;margin:0 auto" />
 
<hr />
<h2>The Rectangle Refactor: Why Structs Actually Matter</h2>
<p>This is where things got concrete for me. I started with this:</p>
<pre><code class="language-rust">fn main() {
    let width1 = 30;
    let height1 = 50;
    println!("Area: {}", area(width1, height1));
}
 
fn area(width: u32, height: u32) -&gt; u32 {
    width * height
}
</code></pre>
<p>It works. But <code>width1</code> and <code>height1</code> are just floating variables with no relationship expressed in code. I refactored through tuples first:</p>
<pre><code class="language-rust">let rect = (30, 50);
 
fn area(dimensions: (u32, u32)) -&gt; u32 {
    dimensions.0 * dimensions.1
}
</code></pre>
<p>Better — one variable now. But <code>dimensions.0</code> vs <code>dimensions.1</code>? Which is width again? The code lost meaning in the process.</p>
<p>The final version with a struct is clearly the best:</p>
<pre><code class="language-rust">#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}
 
fn main() {
    let rect = Rectangle { width: 30, height: 50 };
    println!("Area: {}", area(&amp;rect));
}
 
fn area(rectangle: &amp;Rectangle) -&gt; u32 {
    rectangle.width * rectangle.height
}
</code></pre>
<p>Named fields. A real type. And notice we're passing <code>&amp;rect</code> — a <em>reference</em> — because we want to use the rectangle without taking ownership of it. The function just borrows it.</p>
<hr />
<h2>Printing Structs with <code>#[derive(Debug)]</code></h2>
<p>When I tried to <code>println!</code> a struct, Rust immediately complained:</p>
<pre><code>`Rectangle` doesn't implement `std::fmt::Display`
</code></pre>
<p>Primitive types know how to display themselves. Custom structs don't — there's no single "right" way to format them. But Rust gives you a shortcut for debug-style printing:</p>
<ol>
<li>Add <code>#[derive(Debug)]</code> above the struct</li>
<li>Use <code>{:?}</code> in the format string (or <code>{:#?}</code> for pretty-printed multiline output)</li>
</ol>
<pre><code class="language-rust">#[derive(Debug)]
struct Rectangle { width: u32, height: u32 }
 
println!("rect: {:?}", rect);   // compact
println!("rect: {:#?}", rect);  // pretty
</code></pre>
<p><code>#[derive(Debug)]</code> tells the compiler to auto-generate a Debug implementation for you. You'll use this constantly while building things out.</p>
<hr />
<h2>Methods: Attaching Behavior to Your Struct</h2>
<p>Here's the real power move. That <code>area</code> function is intimately tied to <code>Rectangle</code> — but it's defined separately, hanging out in the global scope. Methods let you attach it directly to the struct using an <code>impl</code> block:</p>
<pre><code class="language-rust">impl Rectangle {
    fn area(&amp;self) -&gt; u32 {
        self.width * self.height
    }
}
</code></pre>
<p>The first parameter is always <code>&amp;self</code> — a reference to the instance the method is called on. Now you call it like:</p>
<pre><code class="language-rust">println!("Area: {}", rect.area());
</code></pre>
<p>Much cleaner. And it makes the relationship explicit: <code>area</code> belongs to <code>Rectangle</code>.</p>
<p>I also wrote a <code>can_hold</code> method that takes another rectangle as an argument:</p>
<pre><code class="language-rust">impl Rectangle {
    fn can_hold(&amp;self, other: &amp;Rectangle) -&gt; bool {
        self.width &gt; other.width &amp;&amp; self.height &gt; other.height
    }
}
</code></pre>
<p>Two parameters: <code>self</code> (the current instance) and <code>other</code> (a borrowed reference to another <code>Rectangle</code>). Usage:</p>
<pre><code class="language-rust">if rect.can_hold(&amp;rect1) {
    println!("Current rectangle can hold the other.");
} else {
    println!("Current rectangle cannot hold the other.");
}
</code></pre>
<p>Rust handles the referencing and dereferencing automatically when you call methods — you don't need different syntax for calling a method on a value vs. a pointer like you would in C++.</p>
<img src="https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExeThocWdlYnRmYzlma3JoM2w5M2M1NDJjeGhoa2tuM2dqaGx2NW85ZiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/MXh5hUhtoUhytIg89s/giphy.gif" alt="it just works magic gif" style="display:block;margin:0 auto" />
 
<hr />
<h2>Associated Functions: The Struct's Static Methods</h2>
<p>Not everything in an <code>impl</code> block needs to be a method. <strong>Associated functions</strong> don't take <code>self</code> as a parameter — they're tied to the <em>type</em>, not to an <em>instance</em>. The most common use is constructors.</p>
<pre><code class="language-rust">impl Rectangle {
    fn square(size: u32) -&gt; Rectangle {
        Rectangle {
            width: size,
            height: size,
        }
    }
}
</code></pre>
<p>You call this with <code>::</code> syntax, not dot notation:</p>
<pre><code class="language-rust">let rect2 = Rectangle::square(2);
</code></pre>
<p><code>String::from(...)</code> is an associated function. <code>Vec::new()</code> is an associated function. Now you know what that <code>::</code> means.</p>
<p>You can have multiple <code>impl</code> blocks for the same struct — Rust allows it. Normally you'd keep everything in one, but it becomes useful with generics and traits (a later chapter).</p>
<hr />
<h2>Bringing It All Together</h2>
<p>Here's the final version of the Rectangle code from my practice session:</p>
<pre><code class="language-rust">#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}
 
impl Rectangle {
    fn area(&amp;self) -&gt; u32 {
        self.width * self.height
    }
 
    fn can_hold(&amp;self, other: &amp;Rectangle) -&gt; bool {
        self.width &gt; other.width &amp;&amp; self.height &gt; other.height
    }
}
 
impl Rectangle {
    fn square(size: u32) -&gt; Rectangle {
        Rectangle {
            width: size,
            height: size,
        }
    }
}
 
fn main() {
    let rect = Rectangle { width: 30, height: 50 };
    let rect1 = Rectangle { width: 20, height: 30 };
    let rect2 = Rectangle::square(2);
 
    println!("rect2: {:#?}", rect2);
    println!("Area: {} square pixels.", rect.area());
 
    if rect.can_hold(&amp;rect1) {
        println!("rect can hold rect1.");
    } else {
        println!("rect cannot hold rect1.");
    }
}
</code></pre>
<p>From two scattered variables to a named type with behavior — that's the struct journey.</p>
<hr />
<h2>Key Takeaways</h2>
<ul>
<li><strong>Structs group related data with named fields</strong>, making your code self-documenting. Prefer them over tuples when fields have distinct meaning.</li>
<li><strong>Methods live in <code>impl</code> blocks</strong> and always take <code>&amp;self</code> (or <code>&amp;mut self</code>) as the first parameter. Call them with dot notation.</li>
<li><strong>Associated functions</strong> (no <code>self</code>) are called with <code>::</code> syntax — use them for constructors and type-level utilities like <code>Rectangle::square(5)</code>.</li>
</ul>
<hr />
<h2>References</h2>
<ul>
<li>The Rust Book, Chapter 5: <a href="https://doc.rust-lang.org/book/ch05-00-structs.html">https://doc.rust-lang.org/book/ch05-00-structs.html</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Rust Ownership Isn't Magic — It's Just Strict Rules You've Never Had to Follow Before]]></title><description><![CDATA[You've written C++ long enough to have a sixth sense for use-after-free bugs. Or maybe you come from Python or JavaScript and you've never had to think about memory at all. Either way, when you first ]]></description><link>https://blog.abisheka.in/rust-ownership-isn-t-magic-it-s-just-strict-rules-you-ve-never-had-to-follow-before</link><guid isPermaLink="true">https://blog.abisheka.in/rust-ownership-isn-t-magic-it-s-just-strict-rules-you-ve-never-had-to-follow-before</guid><category><![CDATA[Rust]]></category><category><![CDATA[rust-ownership]]></category><category><![CDATA[memory-management]]></category><category><![CDATA[Systems Programming]]></category><category><![CDATA[beginnersguide]]></category><dc:creator><![CDATA[Abishek A]]></dc:creator><pubDate>Tue, 12 May 2026 16:26:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/66827a195bf5be060a0dd888/44b6e092-3fcd-4a1b-9b21-fafc97a6b02c.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<p>You've written C++ long enough to have a sixth sense for use-after-free bugs. Or maybe you come from Python or JavaScript and you've never had to think about memory at all. Either way, when you first touch Rust, something weird happens: the compiler yells at you for code that <em>looks completely fine</em>.</p>
<pre><code class="language-rust">let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1); // ❌ compiler error
</code></pre>
<p>"What do you <em>mean</em> <code>s1</code> isn't available? I literally just defined it two lines ago."</p>
<p>That confusion is the exact thing this post will untangle.</p>
<hr />
<h2>The Problem Rust Is Solving</h2>
<p>Before we get into the rules, let's understand <em>why</em> they exist.</p>
<p>In languages with a garbage collector (Java, Python, Go), the runtime tracks all memory references and cleans up when nothing points to a value anymore. It's safe, but it costs runtime overhead and introduces unpredictable pauses.</p>
<p>In C/C++, you manage memory yourself. Powerful, fast — and a landmine field. Use a pointer after the memory is freed? That's a bug. Free the same memory twice? Also a bug. Forget to free? Memory leak.</p>
<p>Rust's answer: enforce memory safety at <strong>compile time</strong>, with zero runtime cost. The mechanism it uses is called <strong>ownership</strong>.</p>
<img src="https://media1.tenor.com/m/WBcY8E7vVCoAAAAd/monkey-computer-not-working.gif" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>The Three Rules (Burn These Into Your Brain)</h2>
<p>Rust's ownership system is built on exactly three rules:</p>
<ol>
<li><p><strong>Each value has exactly one owner</strong> — a variable that "owns" it.</p>
</li>
<li><p><strong>There can only be one owner at a time.</strong></p>
</li>
<li><p><strong>When the owner goes out of scope, the value is dropped</strong> (memory freed). That's it. The entire system flows from these three rules. They're simple to state, but their implications take a while to fully absorb.</p>
</li>
</ol>
<hr />
<h2>Stack vs. Heap: Why It Matters Here</h2>
<p>Here's an analogy: think of the <strong>stack</strong> as a notepad on your desk. You jot down a number, use it, cross it off. Fast, local, self-managing.</p>
<p>The <strong>heap</strong> is more like a storage unit you rent. You put your stuff in, you get a key (a pointer). Someone has to be responsible for returning that key and clearing out the unit when you're done — otherwise you're paying forever for space you're not using.</p>
<p>In Rust:</p>
<ul>
<li><p>Simple scalar types (<code>i32</code>, <code>bool</code>, <code>f64</code>, etc.) live on the <strong>stack</strong>. Copying them is trivially cheap, so Rust just copies them automatically.</p>
</li>
<li><p>Types like <code>String</code> and <code>Vec</code> involve heap allocation. Copying them isn't free, so Rust doesn't do it silently. This is why this works just fine:</p>
</li>
</ul>
<pre><code class="language-rust">let x = 5;
let y = x; // x is copied — integers live on the stack
println!("{}", x); // ✅ x still works
</code></pre>
<p>But this doesn't:</p>
<pre><code class="language-rust">let s1 = String::from("hello");
let s2 = s1; // s1 is *moved* into s2 — Strings live on the heap
println!("{}", s1); // ❌ s1 is gone
</code></pre>
<p>When you assign <code>s1</code> to <code>s2</code>, Rust doesn't copy the heap data — that could be expensive and surprising. Instead, it <strong>moves</strong> the ownership. <code>s1</code> is now considered invalid. There is still only one owner (rule #2), and it's now <code>s2</code>.</p>
<img src="https://media1.tenor.com/m/fe6GNyCsliYAAAAd/this-is-the-key-michael-mendl.gif" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>But What If I Actually Want a Copy?</h2>
<p>Use <code>.clone()</code>. It's explicit, and explicit is good — you're acknowledging "yes, I want to pay the cost of a full deep copy here":</p>
<pre><code class="language-rust">let s1 = String::from("hello");
let s2 = s1.clone(); // deep copy of heap data
 
println!("{} and {}", s1, s2); // ✅ both work
</code></pre>
<p>The explicitness is intentional. In Rust, expensive operations are never silent.</p>
<hr />
<h2>Ownership and Functions</h2>
<p>The same rules apply when you pass values into functions. A function that takes ownership of its argument is like a black hole — the value goes in, and the caller can't use it anymore:</p>
<pre><code class="language-rust">fn main() {
    let s = String::from("hello");
    takes_ownership(s);
    println!("{}", s); // ❌ s was moved into the function
}
 
fn takes_ownership(some_string: String) {
    println!("{}", some_string);
} // some_string is dropped here
</code></pre>
<p>Integers, of course, are fine — they're stack values, so they're always copied:</p>
<pre><code class="language-rust">fn main() {
    let x = 5;
    makes_copy(x);
    println!("{}", x); // ✅ x was copied, not moved
}
 
fn makes_copy(some_integer: i32) {
    println!("{}", some_integer);
}
</code></pre>
<p>You can also get ownership <em>back</em> from a function by returning the value:</p>
<pre><code class="language-rust">fn gives_ownership() -&gt; String {
    let some_string = String::from("hello");
    some_string // moved to the caller
}
 
fn takes_and_gives_back(a_string: String) -&gt; String {
    a_string // moved right back out
}
</code></pre>
<p>This works, but passing ownership in and out just to use a value is clunky. Rust has a better answer.</p>
<blockquote>
<img src="https://media0.giphy.com/media/v1.Y2lkPTc5MGI3NjExcm92ZzJ3eTdzOGN4NW9neXc5anVlNTdqOHcyOWlldWNkeWh3eHNwZyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/sRIVe9E95EtZ6iqzez/giphy.gif" alt="Alt Text" style="display:block;margin:0 auto" /></blockquote>
<hr />
<h2>References: Borrow Without Taking</h2>
<p>Most of the time, you don't want to give up ownership — you just want to <em>use</em> a value for a bit. That's what <strong>references</strong> are for. Passing a reference is called <strong>borrowing</strong>.</p>
<pre><code class="language-rust">fn main() {
    let s1 = String::from("hello");
    let len = calculate_length(&amp;s1); // we're lending s1, not giving it away
    println!("The length of '{}' is {}.", s1, len); // ✅ s1 still here
}
 
fn calculate_length(s: &amp;String) -&gt; usize {
    s.len()
} // s goes out of scope, but since it doesn't own anything, nothing is dropped
</code></pre>
<p>The <code>&amp;</code> means "reference to". The function borrows <code>s1</code>, uses it, and gives it back implicitly when it's done. <code>s1</code> never stopped being owned by <code>main</code>.</p>
<p>Think of it like lending someone your car keys. They can drive your car. They can't sell it. When they're done, you still have it.</p>
<hr />
<h2>Mutable References</h2>
<p>By default, borrowed references are immutable. If you want the borrower to be able to change the value, you need a <strong>mutable reference</strong>:</p>
<pre><code class="language-rust">fn main() {
    let mut s1 = String::from("hello"); // the variable itself must be mut
    change(&amp;mut s1); // pass a mutable reference
}
 
fn change(some_string: &amp;mut String) {
    some_string.push_str(", world");
}
</code></pre>
<p>Two things to note:</p>
<ul>
<li><p>The variable must be declared <code>mut</code>.</p>
</li>
<li><p>You explicitly opt into mutability with <code>&amp;mut</code>.</p>
</li>
</ul>
<hr />
<h2>The Golden Rule of Mutable References</h2>
<p>Here's where Rust gets strict in a way that confuses almost everyone at first: <strong>you can only have one mutable reference to a value at a time.</strong></p>
<pre><code class="language-rust">let mut s = String::from("hello");
 
let r1 = &amp;mut s;
let r2 = &amp;mut s; // ❌ compiler error: cannot borrow `s` as mutable more than once
</code></pre>
<p>And you can't have a mutable reference while an immutable reference is also alive:</p>
<pre><code class="language-rust">let mut s = String::from("hello");
 
let r1 = &amp;s;      // immutable reference
let r2 = &amp;s;      // also fine — multiple immutable references are allowed
let r3 = &amp;mut s;  // ❌ error: can't mutate while immutable references exist
</code></pre>
<p>Multiple immutable references? Fine. Everyone's just reading — no conflict.<br />One mutable reference? Fine. One writer, exclusive access.<br />Mix of both, or multiple mutable references? <strong>Hard no.</strong> This is a data race waiting to happen.</p>
<p>This is Rust preventing a whole class of bugs — the kind that ruin your afternoon in C++ when two threads are both mutating the same object and you get mysterious corruption. Rust makes that <em>impossible to compile</em>.</p>
<blockquote>
<img src="https://media1.tenor.com/m/cxgZ_JabSJcAAAAd/dogs-dabke.gif" alt="Alt Text" style="display:block;margin:0 auto" /></blockquote>
<hr />
<h2>Quick Reference: The Rules in Practice</h2>
<table>
<thead>
<tr>
<th>Situation</th>
<th>Allowed?</th>
</tr>
</thead>
<tbody><tr>
<td>Multiple immutable references (<code>&amp;T</code>)</td>
<td>✅</td>
</tr>
<tr>
<td>One mutable reference (<code>&amp;mut T</code>)</td>
<td>✅</td>
</tr>
<tr>
<td>Multiple mutable references</td>
<td>❌</td>
</tr>
<tr>
<td>Mutable reference + any immutable reference</td>
<td>❌</td>
</tr>
<tr>
<td>Passing a <code>String</code> to a function</td>
<td>Moves ownership (caller loses it)</td>
</tr>
<tr>
<td>Passing <code>&amp;String</code> to a function</td>
<td>Borrows (caller keeps it)</td>
</tr>
<tr>
<td>Copying an integer</td>
<td>Automatic (stack value)</td>
</tr>
<tr>
<td>Cloning a <code>String</code></td>
<td>Explicit deep copy</td>
</tr>
</tbody></table>
<hr />
<h2>The Takeaways</h2>
<p>If you read nothing else, remember these three things:</p>
<ol>
<li><p><strong>Move semantics are the default for heap values.</strong> Assignment, passing to a function — these transfer ownership. If you need the original, <code>.clone()</code> it or use a reference.</p>
</li>
<li><p><strong>References let you use a value without owning it.</strong> The <code>&amp;</code> operator borrows. Mutable borrows need <code>&amp;mut</code> — and the variable itself must also be <code>mut</code>.</p>
</li>
<li><p><strong>Rust's aliasing rules prevent data races at compile time.</strong> One mutable reference <em>or</em> many immutable references — never both at the same time. This feels annoying at first and then feels like a superpower once you've been saved by it. Ownership is the hardest mental shift when learning Rust, and also the most rewarding. Once it clicks, you stop fighting the compiler and start reading its errors like useful hints from a very strict, very helpful colleague.</p>
</li>
</ol>
<hr />
<p><em>Reference:</em> <a href="https://doc.rust-lang.org/book/"><em>The Rust Programming Language (a.k.a. "The Book")</em></a></p>
]]></content:encoded></item></channel></rss>