Ownership in Rust
Articles Blog

Ownership in Rust

Hello, this presentation is about Ownership in Rust Rust is a programming language being developed by Mozilla Let us take a look at some Rust code In this code, we declare ‘a’ equal to some String ‘book’ and print its value. Similarly, we declare a variable ‘b’ and assign the value of ‘a’ to it. and print its value. The output is pretty straightforward. What happens if we try to modify the program by printing the value of ‘a’ at the end of the program? We get an error. This error can be explained if we understand how ownership works in Rust. Suppose you buy a book. You now ‘own’ the book. When your friend buys the book from you the ownership of the book is transferred from you to your friend When you try to read the book in the future, you can’t Because your friend ‘owns’ the book now Applying this knowledge to our program We understand what the problem is Initially, ‘a’ owns the String ‘book’ and then we assign b equals a, the ownership of the String is transferred from a to b. Later on, when we try to print the value of ‘a’ we get an error because ‘a’ does not own the string ‘book’ anymore. This is how ownership in Rust works. Thank you for watching!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top