From the course: Rust Essential Training

Declaring variables - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Declaring variables

- [Narrator] "Hello, World!", was a good first program, but it doesn't do much. So it's time to explore some of the common programming mechanisms we'll need to build more useful Rust applications. Starting with variables. Variables, are used in virtually every program to store and manipulate data. The variable associates, a name in a code with a value held somewhere within the computer's memory. We can declare new variables in Rust using the keyword, let. The basic statement to create a new variable starts with let. I'll add via name for the variable. I'll name this one X for simplicity. After that an equal symbol, which serves as the assignment operator an initial value for the variable. And finally, a semi-colon to end the statement. The let keyword establishes X as a new variable name to use within this scope of our program and the assignment operator hakes what's on the right side of the equal symbol, the valued 10 and assigns it to the variable X on the left side. Now that we've declared X and assigned it a value let's use the print statement on line three to display its value. Or remove the text, "Hello, World!" and replace it with X is open, closed curly braces. And then after the string comma and X for a second input argument. The open and closed curly braces serve as a place holder in the string at the print line macro will populate with the value of our variable X which we specify as a second argument, following the string. If I save and then run this program using the cargo-run command you can see the output message that X is 10. We successfully stored the value 10 in a variable named X and then retrieved that value to using the print statement. Now let's try changing the value of X to something else. After printing it, we'll set X equal to 20 and then I'll copy and paste that print statement of view it's new value. If I save and run this code we encounter our first compiler error. Errors like this are never fun because it means something's broken in our code. But as a programmer, we need to get used to seeing them because compiler errors are part of programming life. Rather than getting upset I like to put a positive spin on the situation and be thankful that the compiler detected something was wrong that could potentially cause problems down the road. Fortunately, the Rust compiler does a great job of explaining why the code won't compile. And in many situations it will even suggest what to do to fix it. The error message in red, has these little red arrows pointing to the fourth line in our program, with the message that we cannot assign twice to an immutable variable. We started by assigning X, the value 10 on line two and then try to assign it a different value on line four. By default variables in Rust are immutable which means they can not be changed or mutated. Once you bind a value to a variable name, it's set. You cannot change it to a different value later. This may seem counterintuitive because the term variable kind of implies something that can change. And in many other common programming languages this default restriction doesn't exist. If you need to change the value of a variable in Rust, you have to explicitly declare that variable as being mutable, using the key word mut. This is one example of how Rust is designed with safety and reliability in mind. By requiring you to make it clear that yes, you intend that variable to be mutable. The compiler is able to protect against situations where one part of your code might declare a variable expecting it to stay constant. And then another part of code that you develop later on makes changes to that variable. That's a common source of software bugs that can be difficult to track down. And Rust does its part to prevent that by requiring variables to be explicitly labeled as mutable. So let's modify this program by declaring the variable X as mutable. I'll save that change, build and run again. And now it works, printing two messages one from when X was 10, and then the second after we changed it to a value of 20. For this example, I named the variable X for simplicity but Rust allows you to use much more than simple single letter variable names. Variable names in Rust, can be composed of a combination of letters, numeric digits, and underscore characters. However, the name cannot begin with a number only a letter or an underscore. Names in Rust are case sensitive so it does differentiate between upper and lower case letters. And finally, you cannot use the same name as reserved keywords, such as let or mut. If you try to name a variable let that will confuse the compiler and you'll get an error. Now, in addition to those rules which the Rust compiler will enforce, the Rust community has also established naming conventions as guidelines, for how to structure the names for different types of items within your program. For example, local variables should be named using what's known as, snake case. Using all lowercase letters with underscores to separate words as needed. The compiler does not enforce these naming conventions but I strongly encourage you to follow them to keep your code consistent with other Rust programs.

Contents