Copy, move, and borrow rules
This topic is not available in the selected language or version. Showing the start of this section.
| Value | Passing by value |
|---|---|
| Numbers and booleans | Copy |
| String and Vec with cloneable elements | Independent copy of contents |
| Box, File, or a type containing a move-only component | Ownership transfer; original becomes unavailable |
Structs, enums, and tuples follow their components. One move-only component makes the entire value move-only. A function return transfers its result to the caller.
References
Section titled “References”&T is shared, &mut T is mutable, and *reference reads the target. A mutable borrow requires a let mut owner. Explicit reference creation and parameters require explc.
While a shared reference remains in use, the original cannot change or move. While a mutable reference remains in use, another reference or the original cannot access that value. Restrictions end after the reference’s last use. References cannot be returned or stored in struct fields. Temporaries cannot be borrowed directly.
Cleanup
Section titled “Cleanup”Owned values are released on block exit, including return, break, continue, and ?. Open Files close automatically. Panic and runtime errors terminate without normal cleanup.
Learn more: Copies, moves, and references