![[Rust Guide] 4.4. Reference and Borrowing](/_next/image?url=https%3A%2F%2Fmedia2.dev.to%2Fdynamic%2Fimage%2Fwidth%3D800%252Cheight%3D%252Cfit%3Dscale-down%252Cgravity%3Dauto%252Cformat%3Dauto%2Fhttps%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Farticles%252F3a1cyzgvc1kfwvn6by5p.png&w=1200&q=75)
[Rust Guide] 4.4. Reference and Borrowing
4.4.0 Before the Main Text This section is actually similar to how C++'s move semantics for smart pointers are constrained at the compiler level. The way references are written in Rust becomes, through compiler restrictions, the most ideal and most standardized way to write pointers in C++. So anyone who has studied C++ will definitely find this chapter very familiar. If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series. 4.4.1 References References let a function use a value without taking ownership of it . When declaring one, add & before the type to indicate a reference. For example, a reference to String is &String . If you have studied C++, the dereference operator in C++ is * , and it is the same in Rust. After learning references, you can simplify the example at the end of the previous article. Here is the previous code: fn main () { let s1 = String :: from ( "hello" ); let ( s2 , len ) = calculate_length ( s1 ); println! ( "The
Continue reading on Dev.to
Opens in a new tab


