From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Mutable references - Rust Tutorial
From the course: Rust Essential Training
Mutable references
- [Instructor] If we want to change the value of a borrowed variable, we have to explicitly tell Rust that that's what we intend to do by marking it as mutable. Let's say we want our process fuel function to modify its borrowed propellant string by extending the message to say that the fuel is highly flammable. This will be modifying the original string declared up on line two. So we'll definitely need to make that mutable. Now, when I run this program, the compiler gives us an error when we try modifying the string. The red text gives us the reason for the error that the data our propellant reference refers to is not borrowed as mutable and the blue text above that, suggest changing it to a mutable reference. Thanks, Rust compiler. Similar to how we have to explicitly declare variables as being mutable, we also have to let Rust know that we intend for references we're borrowing to be mutable. And we do that by…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.