flâneur — a map of the web's best reading

References and Borrowing - The Rust Programming Language

rust-book.cs.brown.edu · 3,558 words · saved by 1 readers

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