Ownership and Borrowing
Every value has one owner, a move transfers it, and a borrow lends it under rules the compiler checks. Interviewers start here because nothing else in Rust makes sense without it.
on this pageshowhide
explore
- Moves and Partial Moves5 questions
- Copy and Clone5 questions
- Drop and RAII5 questions
- References and Aliasing5 questions
- Slices and Borrowed Views5 questions
- Borrow Checker Errors6 questions
questions
page 2 of 2A Rust pool guard must hand its connection back inside Drop and also offer `detach(self)`; why does moving `self.conn` out fail in both, and how do you fix it?
level: seniorimportance: nice to knowfreq 22%
basics
~20 sDrop::drop gets &mut self, so moving a field out is E0507; a by-value method on a Drop type cannot move a field out either (E0509), since drop still needs the whole value. Store Option<Connection> and take() it, or use ManuallyDrop with unsafe take.
showing 31–31 of 31