Borrowing In Rust
In this article, we will learn about borrowing in Rust. In Ownership In Rust , we have learned that only a single variable can hold the ownership of data on the heap at a time. Before starting the discussion of borrowing, we should understand that assigning a variable to another variable, passing a variable as an argument to a function, and assigning the return value of a function to a variable are the same. Another concept of scope is also very important. In Rust, the last use of a variable defines the scope of the variable. In other words, the scope of a variable ends when it is used the last time in a function. When we assign a variable that points to data on the heap to another variable, it transfers ownership of the data to another variable. As we have read above, ownership is also transferred in the case of an argument passing to a function and returning a value from the function. To get rid of the situation, where ownership is transferred to a function and again re...