📜 ⬆️ ⬇️

Graphic description of ownership and borrowing in Rust

Below is a graphic description of moving, copying and borrowing in the Rust programming language . Basically, these concepts are specific only to Rust, being a common stumbling block for many newbies.



To avoid confusion, I tried to keep the text to a minimum. This note is not a substitute for various tutorials, and is only made for those who believe that visually information is perceived more easily. If you are just starting to learn Rust and find these graphics useful, then I would recommend that you mark your code with similar schemes to better consolidate the concepts.



Scheme


The picture is clickable, you can enlarge it. You can also get diagrams without translation in the form of PNG , SVG or PDF .


The top two diagrams depict the two main types of data semantics that are available to us: either moving or copying.



The two central schemes describe two methods of borrowing an object that you own, and what each of these methods offers.



In both schemes, is the name I chose to denote link lifetime . I specifically used the Greek letter, since, at the moment, there is no syntax to describe the specific lifetimes in Rust.


The last two schemes summarize, showing the main differences and similarities between the two types of links, both in the form of an image and in the form of text. The qualifier "externally" is important, since you can have internal variability through things like Cell .


Translator's Note


I would like to express a special thanks to Andrey Lesnikov ( @ozkiff ), Serhii Plyhun ( @ snuk182 ) and Sergey Veselkov ( @vessd ) for their help in translating and following the review.


')

Source: https://habr.com/ru/post/322140/


All Articles