Posts

Showing posts with the label heap

Ownership In Rust

Image
    In this article, we will learn about ownership in Rust. fn main () {     let i = 5 ; //on stack     let j = i ; //on stack     println! ( "i = {i}, j = {j}" );       let str = String :: from ( "theloveoftechI" );     let str1 = str . clone ();     println! ( "{str}, {str1}" );       let mv_str = str ;     println! ( "{mv_str}" );     //println!("{str}"); //This is problematic }   Variables are allocated on the stack or on the heap. We can only push an item on the top of the stack. We can also pop an item from the top of the stack. Variables on the stack have fixed sizes. All variables are allocated on the stack when a function is called. In the case of a heap, space is allocated based on the size of the data. If we want to grow a particular vegetable in the garden, so we reserve a certain area for that vegetable. In the case of i and j, both are on the stack ...