Let's talk about variables. They're pretty important. They look like this first. Notice that rust is a braced language with statements terminated by semi colons. Second, rust intentionally mimics as much syntax from other languages as it can C and Python in particular. So it should feel familiar right from the start for a lot of you.
To declare a variable, you use a let statement. Here we declare bunnies and initialize it to the integer value of two. Rust is a strongly typed language. So where is the type annotation? Well, whenever rust can figure out the appropriate type for you, you can just leave the type annotation out. You don't have to write auto or anything.
When you do annotate the type, it looks like this a colon after the variable name and then the type is 32 is assigned 32 bit integer. We'll talk about primitive types in a later video. So that's how you initialize a single variable. It just so happens that the let statement has a trick up its sleeve that will allow us to initialize multiple variables at once. A let statement can D structure the data on the right hand side and use it to initialize variables inside of a corresponding pattern on the left hand side. We'll talk a little bit more about patterns in a later video as well.
For now, we'll settle for just using a tuple pattern when when we want to initialize multiple variables at once. Variables are immutable by default in rust, which means unless you choose to make them mutable, you can't ever change their value. That's unusual. Most languages default to variables that you can change why would rust have immutable variables by default, safety, concurrency and speed. There are lots of bugs that can't happen if a value never changes. So safety is improved by immutability.
Data that never changes can be shared between multiple threads without locks. So concurrency is improved by immutability. The compiler can also do extra up summarizations on data it knows won't change. So speed is improved by immutability. That's why data is immutable by default and rust, safety, concurrency and speed. But let's face it, sometimes we need to change the value of a variable, we can't just change the value of bunnies as long as it is immutable, that would give us an error, this error.
In fact, here's our very first example of our friendly compiler nudging us in the right direction. Let's break down this information. At the top we have a summary of the error cannot assign twice to immutable variable bunnies. Next, we have the location of the error source main.rs, line three, column five. Then most amazingly, we have a contextual explanation of what we are doing wrong and how we might fix it. On line two we initialize an immutable bunnies to the value 16.
Therefore online three, we cannot assign a different value immutable means you can't change it. What does the compiler suggest, make the variable online to mutable. If this information isn't enough to figure out the error, there is also a full explanation of this error type that you can get by running this command, you can go ahead and run this command right now if you like. Rust C is the actual compiler that cargo uses under the hood to compile your code. Here's the version of the code that will work it looks like this. Let mute bunnies equal 32.
Okay, so we have mutable variables that we can change and immutable variables that we cannot change. But there's also a kind of variable that is even immutable or the constant. There are four things different about declaring a constant first const instead of let second. The convention is to use screaming snake case for constants, meaning all uppercase words separated by underscores 30 The type annotation is required, you can't just leave it out and forth, the value must be a constant expression that can be determined at compile time, the rust team is adding more and more standard library functions to the constant list every release. So there's actually quite a bit of things you can do at compile time. That counts as a constant expression.
A literal always works just fine. So why would you go to all this trouble to use a const? Two reasons. First, you can place a constant outside of a function at module scope and use it anywhere you want a global immutable, constant global second, because const values are inlined at compile time, they are really fast. In the next video, we'll talk about scope.