In this lecture, we are going to start our journey by learning about variables.
This course assumes you have some experience with programming.
Learning a new language is much easier the second time around, while the syntax may be different.
The concepts are very similar from language to language.
Variables are still variables.
Functions are still functions.
Conditional statements are still conditional statements, so on and so forth.
You get the picture variables in rust serve the same purpose as variables in other languages.
Their containers for data throughout this course, we will be working inside these source slash main
RC file.
I recommend opening this file before starting a lecture.
Inside this file cargo has defined a function called Main.
I know we said we would start with variables, but let me quickly introduce functions.
Functions are blocks of code for performing a particular task.
If you have a background in JavaScript, you would use the function key word to define a function for
Python developers.
You would use the defined keyword for US developers.
Function definitions start with the FN keyword.
It's all the same.
The FN keyword is followed by the name of the function.
A list of arguments and then the lock of code.
Our apps must define a function called mean.
By default, Cargoe will search for a function called mean during compilation.
If we were to rename this function, the compiler would complain.
It's always guaranteed this function will run.
Once we're going to empty, the contents of the function will be starting fresh.
The main focus of this lecture is variables.
We can declare a variable by typing the let keyword.
Next, we can give our variable a name for this example.
Let's set the name to my name for the value.
We're going to store our real name or whatever name you prefer.
After declaring this variable rust is going to throw a warning.
Assuming you have the rust extension enabled, there should be a yellow squiggly line below the variable
name.
We can hover our mouse over the variable to learn about the error just in case.
If you don't see an error, you can produce the error by running the cargo run command in the terminal.
The compiler won't hesitate to output the same error regardless.
The error message should be the name, according to the compiler.
There's a problem with the variable name.
At times, rust can be an opinionated language.
Variables must be snake case.
They can't be camel cased.
It's a standard practice in rust projects.
Let's convert the variable name to snake casing.
Learning to adopt a specific formatting practice can be tedious, especially if you're used to writing
code differently.
However, it's worth it to be consistent with the community.
There are a couple more features I want to point out.
Firstly, statements must end with a semicolon.
If you have a background in JavaScript or Python, semicolons are optional.
Statements in rust must end with a semicolon to indicate the end of the line.
If we were to remove this character, the compiler would throw an error.
Errors are indicated in the editor with a red squiggly line, according to the compiler.
It expected a semicolon.
Let's add it back in.
Moving along, another warning gets thrown by the compiler surrounding the variable.
The error will tell us the variable is unused.
For this example, we are going to ignore this warning.
It's not the end of the world.
All right.
One more thing before we move on to the next topic variables are immutable.
Mutability is another way of saying a variable can't be changed once a variable is initialized.
We can't change it to another value, therefore they're immutable.
This design forces developers to be aware of how their variables will be used.
That doesn't mean mutability is bad.
It is possible to make a variable mutable.
Let's try mutating the my name variable to a different value.
An error gets thrown by the compiler.
It's telling us we can't modify an immutable variable.
Variables can become mutable by adding the multi keyword before the variable name.
Let's add this keyword to the my name variable.
With a single keyword, the air has gone away.
Mutable variables must be explicitly declared with the mute keyword.
You should keep this in mind when working with variables.
Let's continue talking about rust in the next lecture.