<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vedant Navale</title>
    <description>The latest articles on Forem by Vedant Navale (@agecoder).</description>
    <link>https://forem.com/agecoder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3463244%2F20894219-4747-47a0-8100-60ad1523a881.png</url>
      <title>Forem: Vedant Navale</title>
      <link>https://forem.com/agecoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/agecoder"/>
    <language>en</language>
    <item>
      <title>Ownership and Borrowing: My Journey Into Rust’s Unique Memory System</title>
      <dc:creator>Vedant Navale</dc:creator>
      <pubDate>Wed, 27 Aug 2025 17:09:53 +0000</pubDate>
      <link>https://forem.com/agecoder/ownership-and-borrowing-my-journey-into-rusts-unique-memory-system-3nbm</link>
      <guid>https://forem.com/agecoder/ownership-and-borrowing-my-journey-into-rusts-unique-memory-system-3nbm</guid>
      <description>&lt;p&gt;So,I’ve been diving into Rust, and one thing that keeps popping up everywhere is this idea of ownership and borrowing.&lt;br&gt;
At first, it felt like Rust was being too strict with me 😅 errors everywhere! But then I realized… these rules are the secret sauce that make Rust memory safe without a garbage collector.&lt;/p&gt;

&lt;p&gt;I tried to note down the important rules (12 of them) that I keep in mind while coding, along with examples. Writing them out helps me remember and maybe it’ll help you too if you’re learning like me.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 1: Every value has exactly one owner
fn main() {
    let s = String::from("hello");
    println!("{}", s); // s owns "hello"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable s owns the string. Simple start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 2: When the owner goes out of scope, the value is dropped
fn main() {
    {
        let s = String::from("hello");
    } // s goes out of scope -&amp;gt; memory freed automatically
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No free() needed. Rust does it for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 3: Assigning moves ownership
fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // ownership moved
    // println!("{}", s1); // ❌ error: s1 not valid anymore
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was one of the first “gotchas” for me. After s1 moves, it’s gone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 4: Clone makes a deep copy
fn main() {
    let s1 = String::from("hello");
    let s2 = s1.clone();
    println!("{} and {}", s1, s2); // ✅ both valid
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if I really need two separate copies, I clone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 5: Primitives are Copy
fn main() {
    let x = 10;
    let y = x; // just copied
    println!("{} and {}", x, y); // ✅ works fine
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This explains why integers never gave me move errors 😅.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 6: Borrow with &amp;amp;
fn main() {
    let s = String::from("hello");
    print_length(&amp;amp;s); // borrow, no move
    println!("Still valid: {}", s); // ✅
}

fn print_length(s: &amp;amp;String) {
    println!("Length: {}", s.len());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, ownership stays with s, but the function can still read it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 7: Mutable references need &amp;amp;mut
fn main() {
    let mut s = String::from("hello");
    change(&amp;amp;mut s);
    println!("{}", s); // "hello world"
}

fn change(s: &amp;amp;mut String) {
    s.push_str(" world");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first time I used &amp;amp;mut, it clicked  borrowing but with permission to change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 8: Only one mutable reference at a time
fn main() {
    let mut s = String::from("hello");
    let r1 = &amp;amp;mut s;
    // let r2 = &amp;amp;mut s; // ❌ not allowed
    println!("{}", r1);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust says: “No data races allowed here.”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 9: Multiple immutable references are fine
fn main() {
    let s = String::from("hello");
    let r1 = &amp;amp;s;
    let r2 = &amp;amp;s;
    println!("{} and {}", r1, r2); // ✅ works
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes sense  if nobody’s changing it, multiple readers are fine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 10: But you can’t mix mutable and immutable references
fn main() {
    let mut s = String::from("hello");
    let r1 = &amp;amp;s;
    let r2 = &amp;amp;s;
    // let r3 = &amp;amp;mut s; // ❌ clash
    println!("{} and {}", r1, r2);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Took me a while to accept this. Rust protects me even from myself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🦀 Rule 11: References must always be valid
fn main() {
    let r;
    {
        let s = String::from("hello");
        r = &amp;amp;s;
    }
    // println!("{}", r); // ❌ dangling reference
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what other languages sometimes allow… but leads to bugs. Rust just stops me.&lt;/p&gt;

&lt;p&gt;🦀 Rule 12: These rules together = safety&lt;/p&gt;

&lt;p&gt;At first these rules feel like handcuffs. But the more I code, the more I see the benefits:&lt;/p&gt;

&lt;p&gt;No null pointers&lt;br&gt;
No dangling references&lt;br&gt;
No data races in multi-threaded code&lt;br&gt;
And all of this checked at compile time.&lt;/p&gt;

&lt;p&gt;🎯 My Takeaway&lt;br&gt;
I used to fight with the borrow checker, but now I think of it as a teacher. The compiler forces me to think about ownership. And honestly, that’s making me a better programmer.&lt;/p&gt;

&lt;p&gt;I’m still learning, but these 12 rules are like my “cheat sheet” for Rust’s ownership model. Hopefully this helps someone else who’s starting out too 🙌.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>rust</category>
    </item>
  </channel>
</rss>
