<?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>DEV Community: Canhassi</title>
    <description>The latest articles on DEV Community by Canhassi (@canhassi).</description>
    <link>https://dev.to/canhassi</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%2F1002062%2Fee5db6f3-91b1-4426-adf7-cf1028e36e9e.jpeg</url>
      <title>DEV Community: Canhassi</title>
      <link>https://dev.to/canhassi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/canhassi"/>
    <language>en</language>
    <item>
      <title>How Rust memory management work to beginners.</title>
      <dc:creator>Canhassi</dc:creator>
      <pubDate>Thu, 14 Sep 2023 18:25:28 +0000</pubDate>
      <link>https://dev.to/canhassi/how-rust-memory-management-work-to-beginners-622</link>
      <guid>https://dev.to/canhassi/how-rust-memory-management-work-to-beginners-622</guid>
      <description>&lt;p&gt;Do you ever thought about &lt;strong&gt;what happens&lt;/strong&gt; to your &lt;strong&gt;RAM&lt;/strong&gt; when you &lt;strong&gt;run a program&lt;/strong&gt;? And how the &lt;strong&gt;ways you write code can interfere&lt;/strong&gt; with many other things in your system?&lt;/p&gt;

&lt;p&gt;That article will help you to understand more about &lt;em&gt;management memory&lt;/em&gt; and &lt;strong&gt;HOW RUST WORKS THIS SUBJECT&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1. Stack and Heap&lt;/li&gt;
&lt;li&gt;2. Borrow checker&lt;/li&gt;
&lt;li&gt;3. Errors prevention&lt;/li&gt;
&lt;li&gt;4. Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Stack and Heap
&lt;/h2&gt;

&lt;p&gt;Before you learn what rust do, you have to learn some concepts. In general we have two types of memory, that are called: &lt;strong&gt;Stack and Heap&lt;/strong&gt;. Now let's introduce them.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1 Memory: Stack
&lt;/h3&gt;

&lt;p&gt;Stack, as the name says, &lt;strong&gt;works like a stack&lt;/strong&gt;, following the principle of &lt;strong&gt;"Last In, First Out" (LIFO).&lt;/strong&gt;  Maybe it will be easier explain it in steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine a &lt;strong&gt;stack of dishes&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;first dish&lt;/strong&gt; you put in is the &lt;strong&gt;last to be removed&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;When a &lt;strong&gt;function is called&lt;/strong&gt;, a block of memory is &lt;strong&gt;'stacked'&lt;/strong&gt; on top of the stack;&lt;/li&gt;
&lt;li&gt;When the &lt;strong&gt;function ends&lt;/strong&gt;, this block is &lt;strong&gt;"unstacked"&lt;/strong&gt;, freeing this memory. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Normally, the &lt;strong&gt;values&lt;/strong&gt; that will be stored on the stack is known by the &lt;strong&gt;compiler&lt;/strong&gt; (in the compiling time), because it knows how much memory is needed to store. This process occurs &lt;strong&gt;automatically&lt;/strong&gt;, all values are deleted from memory. &lt;/p&gt;

&lt;p&gt;Below is an example of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// at this moment the variable has created&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 12 &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// When the owner (main function) goes out of scope, the value will be dropped&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In Rust we can create some scopes just by using &lt;code&gt;{}&lt;/code&gt;,  and this adds layer into our stack with a limited lifetime. After your goes out from that specific &lt;em&gt;scope&lt;/em&gt;, the memory is cleared and you lost the information related. A good and simple example of it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Cannot find value `number` in this scope&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1.2 Memory: Heap
&lt;/h3&gt;

&lt;p&gt;In a few words: the &lt;strong&gt;heap&lt;/strong&gt; memory is a free memory space to allocate data that may change. &lt;/p&gt;

&lt;p&gt;Imagine that you need to &lt;strong&gt;store some variable&lt;/strong&gt; after starting your program, this variable doesn't have a fixed size known at compile time since it can have size variations or is a direct allocation in memory. &lt;/p&gt;

&lt;p&gt;If one of the possibilities above matches, we know that we have &lt;strong&gt;Heap&lt;/strong&gt; memory instead &lt;strong&gt;Stack&lt;/strong&gt;. Heap has a more &lt;strong&gt;flexible memory&lt;/strong&gt; and a large space. Take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// alocate a integer in heap&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Canhassi"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// alocate a String in heap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Rust we have two &lt;code&gt;types&lt;/code&gt; of string, &lt;code&gt;&amp;amp;str&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&amp;amp;str&lt;/strong&gt;: has a fixed size based on the text written;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;String&lt;/strong&gt;: has a size that can be increased, decreased, and deleted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;... and that's why &lt;code&gt;String&lt;/code&gt; is stored on the Heap, and &lt;code&gt;&amp;amp;str&lt;/code&gt; on the stack.&lt;/p&gt;

&lt;p&gt;One way to free heap memory is: when a &lt;strong&gt;variable that stores&lt;/strong&gt; something that is on the heap &lt;strong&gt;leaves the scope&lt;/strong&gt; of the function (reaches the end of the function), it will be freed in the same way as the stack.&lt;/p&gt;

&lt;p&gt;Okay, now we have a clear image on both memory types, but what the difference to manage memory between Stack and Heap? Let's see these diferences!&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Borrow checker
&lt;/h2&gt;

&lt;p&gt;Borrow Checker is the &lt;strong&gt;part of the Rust compiler&lt;/strong&gt; that &lt;strong&gt;checks and ensures&lt;/strong&gt; that ownership, borrowing, and lifetime &lt;strong&gt;rules are respected&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I'm gonna be honest with you: I had a some issues to understanding how this works at beginning, and I see that is common among new Rustaceans . But don't worry, friend of mine. I'll teach you on the best way possible. But first, let's take a look the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Canhassi"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Creating a string variable&lt;/span&gt;

    &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// calling the print_name function passing the variable&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: name borrowed to print_name()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// print "Canhassi"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the compiler with that code you gonna see the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;borrow of moved value: name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call the &lt;code&gt;print_name&lt;/code&gt; function, the &lt;code&gt;name&lt;/code&gt; variable will be &lt;strong&gt;moved to another scope&lt;/strong&gt; and that scope will be the &lt;strong&gt;new owner&lt;/strong&gt; of it. And the rule of owner goes out scope will applied again. Borrow Checker ensures that &lt;strong&gt;once ownership is transferred&lt;/strong&gt;, the original variable &lt;strong&gt;can no longer be used&lt;/strong&gt; to access that value. this happened in the code above.&lt;/p&gt;

&lt;p&gt;Another way that you can use the original variable is using references like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Canhassi"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Creating a string variable&lt;/span&gt;

    &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// calling the print_name function passing the variable&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// print "Canhassi"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// print "Canhassi"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PS: These rules of &lt;strong&gt;borrow checker work&lt;/strong&gt; just with &lt;strong&gt;objects allocated in heap&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Errors prevention
&lt;/h2&gt;

&lt;p&gt;The Borrow Checker is fundamental to ensuring Rust's memory safety without the need for a &lt;a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)"&gt;garbage collector&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In other languages like C and C++, we (as developers) have to deallocate memory manually with some functions.  C++ is known for allowing memory management errors, including &lt;a href="https://en.wikipedia.org/wiki/Memory_leak"&gt;memory leaks&lt;/a&gt;. C++ itself does not cause memory leaks. Instead, &lt;strong&gt;C++ provides a great deal of flexibility&lt;/strong&gt; and control to the programmer, and this &lt;strong&gt;freedom can lead to errors&lt;/strong&gt; if not used correctly.&lt;/p&gt;

&lt;p&gt;A famous error is &lt;strong&gt;Undefined Behavior&lt;/strong&gt;, for example, imagine a function that return e reference of a variable like that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// calling foo function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// creating a var number&lt;/span&gt;

    &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="c1"&gt;// try return a reference of var number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code doesn't work because the Rust compiler has restrict rules with memory management. We can't return a reference of var &lt;code&gt;number&lt;/code&gt; because the scope of this function will go out, and the var &lt;code&gt;number&lt;/code&gt; will disappear, so Rust doesn't let this happen. In C++ we can do that, and it allows the notorious memory leaks. &lt;/p&gt;

&lt;p&gt;I think that is kinda cool to know that the Rust compiler avoids this type of error... If this subject is new for you, I can tell that memory leaks can cost a lot of money and is truly hard to fix it, since is never only one memory leak.&lt;/p&gt;

&lt;p&gt;Other example is, &lt;strong&gt;Double free&lt;/strong&gt;, that occur when you try free twice the same object for example in C code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This subject is really extensive and there's many possibilities to generate errors like that when you're working in other languages. But, I'll let you do your own research and make sure to tell me more about on this post comments!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Undefined_behavior"&gt;Undefined Behavior&lt;/a&gt;&lt;br&gt;
&lt;a href="https://heap-exploitation.dhavalkapil.com/attacks/double_free"&gt;Double Free&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Conclusion
&lt;/h2&gt;

&lt;p&gt;I wrote this article with the aim of presenting in a more general way how memory management works with Rust. But I recommend for a more complete knowledge the chapter 4 of &lt;a href="https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html"&gt;The Rust Programming Language book&lt;/a&gt;. There you will have more examples and more explanations of this content.&lt;/p&gt;

&lt;p&gt;I really hope this article was helpful!! 🦀🦀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/CanhassiDesu"&gt;My twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/canhassi/"&gt;Linkedln&lt;/a&gt;&lt;/p&gt;

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