Borrowing with Functions
As part of borrow checking, the compiler needs to reason about how borrows flow into and out of functions. In the simplest case borrows last for the duration of the function call:
fn borrows(x: &i32) { dbg!(x); } fn main() { let mut val = 123; // Borrow `val` for the function call. borrows(&val); // Borrow has ended and we're free to mutate. val += 5; }
This slide should take about 3 minutes.
- In this example we borrow
valfor the call toborrows. This would limit our ability to mutateval, but once the function call returns the borrow has ended and we’re free to mutate again.