<?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: itzlg | cyborg</title>
    <description>The latest articles on Forem by itzlg | cyborg (@lovegupta).</description>
    <link>https://forem.com/lovegupta</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%2F788684%2F201cd121-d789-4eea-a3cb-c0bc340000c5.jpg</url>
      <title>Forem: itzlg | cyborg</title>
      <link>https://forem.com/lovegupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lovegupta"/>
    <language>en</language>
    <item>
      <title>Rust Ownership Explained: Why Some Values Move and Others Copy</title>
      <dc:creator>itzlg | cyborg</dc:creator>
      <pubDate>Mon, 01 Sep 2025 21:17:40 +0000</pubDate>
      <link>https://forem.com/lovegupta/rust-ownership-explained-why-some-values-move-and-others-copy-4ol2</link>
      <guid>https://forem.com/lovegupta/rust-ownership-explained-why-some-values-move-and-others-copy-4ol2</guid>
      <description>&lt;p&gt;When we assign a value to a new variable in Rust, the value is usually moved. But here’s the catch ,this depends on the type of the value.&lt;/p&gt;

&lt;p&gt;Rust plays a little trick when you assign values: some get copied, others get moved, depending on their type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kzjavxdw4i0ob1aui85.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kzjavxdw4i0ob1aui85.jpg" alt="rust funny image" width="600" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitive Types (Copy)
&lt;/h2&gt;

&lt;p&gt;For primitive types like integers, booleans, and floats, the value is not moved. Instead, it is copied.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They implement the Copy trait.&lt;/li&gt;
&lt;li&gt;They are stored on the stack.&lt;/li&gt;
&lt;li&gt;They have a fixed size known at compile time.&lt;/li&gt;
&lt;li&gt;Copying them is extremely cheap and efficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5;
let y = x; // y gets a copy, x is still valid
println!("x = {}, y = {}", x, y);

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

&lt;/div&gt;



&lt;p&gt;Both x and y remain valid because i32 or u32 implements Copy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complex Types (Move)
&lt;/h2&gt;

&lt;p&gt;For types like String, Vec, and most custom structs, Rust does not copy the value by default, it moves it.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are stored in the heap.&lt;/li&gt;
&lt;li&gt;Their size is not fixed at compile time.&lt;/li&gt;
&lt;li&gt;Copying them would require duplicating potentially large amounts of data, which is expensive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s1 = String::from("hello");
let s2 = s1; // ownership moved
// println!("{}", s1); // ❌ Error: s1 is no longer valid

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Avoid Moving
&lt;/h2&gt;

&lt;p&gt;Sometimes, you don’t want to lose access to the original variable . Rust gives you two options:&lt;/p&gt;

&lt;h3&gt;
  
  
  Clone it:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);

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

&lt;/div&gt;



&lt;p&gt;This creates a deep copy of the value (including nested values if present) and allocates new memory in the heap which is safe but potentially expensive&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: (If it’s not a String, e.g., a struct, you can still call .clone() as long as the type implements the Clone trait.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Borrow (use references):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s1 = String::from("hello");
let s2 = &amp;amp;s1; // borrow instead of move
println!("s1 = {}, s2 = {}", s1, s2);

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Copy vs Clone
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Copy:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shallow copy&lt;/li&gt;
&lt;li&gt;For small, fixed-size types stored on the stack&lt;/li&gt;
&lt;li&gt;Very cheap&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Clone:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Deep copy&lt;/li&gt;
&lt;li&gt;For complex types stored on the heap&lt;/li&gt;
&lt;li&gt;Allocates new memory, can be expensive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust’s ownership model might feel strict at first, but once you understand the difference between Move, Copy, and Clone, things start to click. The rules are there to help you write safe and efficient code without worrying about memory bugs.&lt;/p&gt;

&lt;p&gt;If you found this helpful, please leave a comment or reaction 👏 it really motivates me to share more Rust content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77fxsdw29ltctrmk47ht.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77fxsdw29ltctrmk47ht.gif" alt="leave comment" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also connect with me on X (Twitter): @&lt;a href="https://x.com/MeLovegupta" rel="noopener noreferrer"&gt;melovegupta&lt;/a&gt; 🚀&lt;/p&gt;

</description>
      <category>rust</category>
      <category>rusttraits</category>
      <category>web3</category>
      <category>memory</category>
    </item>
    <item>
      <title>How the JavaScript code works behind the scene ?</title>
      <dc:creator>itzlg | cyborg</dc:creator>
      <pubDate>Thu, 11 May 2023 19:40:40 +0000</pubDate>
      <link>https://forem.com/lovegupta/how-the-javascript-code-works-behind-the-scene--odg</link>
      <guid>https://forem.com/lovegupta/how-the-javascript-code-works-behind-the-scene--odg</guid>
      <description>&lt;p&gt;I have seen a lot of people who know frontend but don't know very well how JavaScript works. In this post, I will share my learning so that if anyone asks you about this topic, or if an interviewer asks you a question related to this topic, you will not be confused. Let's start! :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;W&lt;/strong&gt;hen we run our &lt;strong&gt;JavaScript&lt;/strong&gt; code , at that time JavaScript engine execute that code in two phases that is&lt;br&gt;
1-creation phase&lt;br&gt;
2-execution phase&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In &lt;strong&gt;creation phase&lt;/strong&gt; , memory is allocated to all the variables and functions , for variables a special value &lt;strong&gt;Undefined&lt;/strong&gt; is assigned to all &lt;strong&gt;variables&lt;/strong&gt; and for &lt;strong&gt;function&lt;/strong&gt; entire code gets copied in memory,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in &lt;strong&gt;execution phase&lt;/strong&gt; js code is executed line by line and in this phase undefined value of variable is replaced by actual value that we were assigning in that variables&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for ex:- let a=20 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;so firstly in creation phase memory is allocated to variable &lt;strong&gt;a&lt;/strong&gt; with undefined value &lt;br&gt;
after that in execution phase value 20 is assigned to &lt;strong&gt;a&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pjsg6b85s4pns9hkr4a.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pjsg6b85s4pns9hkr4a.jpeg" alt="wait a minute guys" width="260" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here very interesting topic comes in picture that is &lt;strong&gt;hoisting&lt;/strong&gt; .&lt;br&gt;
In Hositing you can use a variable or function before it is declared, and JavaScript will still be able to find it.&lt;/p&gt;

&lt;p&gt;However, variables created using the 'let' and 'const' keywords are also present in the TDZ (temporal dead zone), so we cannot access those variables before defining or declaring them. Attempting to do so will result in an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; console.log(a);
let a=20;
output :you can not access it before declaring 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if we use the 'var' keyword with variables, the variables that are created using the 'var' keyword do not exist in the TDZ. Therefore, accessing them before declaring or defining them will not result in an error, but will instead show 'undefined'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a);
var a=20;
ouput: undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But It's generally a good practice to declare all variables and functions at the top of their respective scopes&lt;br&gt;
I hope you will get useful information and I welcome your suggestion 😀 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
