Mutable References

We'll cover the following...

We want to be able to modify the fruit using the increase_fruit function. To make this work, we need to introduce a second kind of reference: a mutable reference. While an immutable reference is &, a mutable reference is &mut. It looks like this:

Press + to interact
fn increase_fruit(fruit: &mut Fruit) {
fruit.apples *= 2;
fruit.bananas *= 3;
}

Note that we put the mut after the & symbol, not before the fruit parameter name. We’ll give a little ...

Access this course and 1400+ top-rated courses and projects.