Borrowing a Value

As we saw before, instead of transferring ownership when calling a function, you can let a function borrow the value:

struct Point(i32, i32);

fn add(p1: &Point, p2: &Point) -> Point {
    Point(p1.0 + p2.0, p1.1 + p2.1)

fn main() {
    let p1 = Point(3, 4);
    let p2 = Point(10, 20);
    let p3 = add(&p1, &p2);
    println!("{p1:?} + {p2:?} = {p3:?}");
  • The add function borrows two points and returns a new point.
  • The caller retains ownership of the inputs.
This slide should take about 5 minutes.

This slide is a review of the material on references from day 1, expanding slightly to include function arguments and return values.