How do I change a user's email address in a plugin?

I have a form where a logged in user can have a confirmation email sent to them. I’m showing an email input field with the email address from their profile prefilled. However, if they change it, I’d like to update their user profile mail address as well.

I have tried:

if ($form->data['email'] != $user['email']) {
    $user->email = $form->data['email'];
}

This does change the email address, but I think it doesn’t write it to the user’s yaml file, because after logging out and back in again, the old one is back. So how can I permanently save that?

Try $user->save()
Can’t test right now and don’t have the form set up, but I hope this should work- I see save() method in User object

Excellent memory, it is as simple as that! Thanks :smiling_face_with_three_hearts: