From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
The borrow checker - Rust Tutorial
From the course: Rust Essential Training
The borrow checker
- [Instructor] Earlier in this course, when discussing references and borrowing, we saw that the Rust compiler can detect when your program tries to use a reference, after the thing at points do has gone out of scope. That's known as a dangling reference. Consider the example program shown here, which declares a variable named propellant on line two without initializing it, who's scope is the body of the main function. After that, a pair of curly braces enclose the code on lines four through six, which therefore exists in an inner sub scope. Those three lines initialize a new string variable named rp1, assign the propellant variable a reference to that new rp1 string and then print propellant. Since everything from the creation of rp1 to its final use takes place within that inner code block, if I run this program, everything works fine and we get the output message that propellant is rp1. However, if we move that final…
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.