References and Borrowing - The Rust Programming Language
Ownership, boxes, and moves provide a foundation for safely programming with the heap. However, move-only APIs can be inconvenient to use. For example, say you want to read some strings twice: In this example, calling greet moves the data from m1 and m2 into the parameters of greet. Both strings are dropped at the end of greet, and therefore cannot be used within main. If we try to read them like in the operation format!(..), then that would be undefined behavior. The Rust compiler therefore rejects this program with the same error we saw last section: This move behavior is extremely inconvenient. Programs often need to use a string more than once. An alternative greet could return ownership of the strings, like this: However, this style of program is quite verbose. Rust provides a concise style of reading and writing without moves through references. A reference is a kind of pointer. Here’s an example of a reference that rewrites our greet program in a more convenient manner: The expr
References and Borrowing - The Rust Programming Language Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help Auto Light Rust Coal Navy Ayu The Rust Programming Language References and Borrowing Ownership, boxes, and moves provide a foundation for safely programming with the heap. However, move-only APIs can be inconvenient to use. For example, say you want to read some strings twice: In this example, calling greet moves the data from m1 and m2 into the parameters of greet . Both strings are droppe
Explore this link on the map →related reading
- References and Borrowing - The Rust Programming Languagedoc.rust-lang.org
- What is Ownership? - The Rust Programming Languagedoc.rust-lang.org
- Rust Language Cheat Sheetcheats.rs
- A half-hour to learn Rustfasterthanli.me
- What is Ownership? - The Rust Programming Languagerust-book.cs.brown.edu
- The Slice Type - The Rust Programming Languagedoc.rust-lang.org
- Moves - Rust: A Better C++ Than C++thecodedmessage.com
- Moves, cloning, and copying - Rust for C-Programmersrust-for-c-programmers.com
- Validating References with Lifetimes - The Rust Programming Languagedoc.rust-lang.org
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org
- Destroy All Values: Designing Deinitialization in Programming Languages - Faultloregankra.github.io
- Rewriting Bun in Rust | Bun Blogbun.com