This content originally appeared on DEV Community and was authored by Chris Noring
TLDR; this is the second part on Rust. In this part we'll cover variables, how to work with them, change values and so on.
Series:
- Your first program
- Variables, you are here
- Functions
- IO, read and write from the console
- Rust projects with Cargo
- Control flow
- Error handling
- Working with files Advanced
- Ownership & Borrowing
- Testing
Resources
Why variables
We use variables to store a value that we want to use later. Additionally, having a variable, a named reference, we're able to better understand what's going on. Here's an example:
let account_balance = 4000;
println!("Your account balance {}", account_balance);
Imagine you didn't have account_balance
but referred to 4000 in your code, you wouldn't know at first glance what 4000 is about.
Declare variables
To declare a variable in Rust, we need to give it a name and a value, like so:
let name = "Chris";
Note how we start with the keyword let
, a name for the variable name
, and a value, "Chris".
Type is inferred by value
Values in Rust have type, and this type is decided at first assignment of a value to the variable. In the below case, name
is of type string and age
of type i32
an integer type:
let name = "Chris";
let age = 20;
When a variable gets a type like this, we call it as type is inferred, meaning that Rust observes what value it's being assigned and makes conclusions based on the assignment.
The type it's inferred is a default type. So numbers, lacking fraction becomes i32
, 32 bit integer (whole numbers lacking decimals) for example. If you want to check what default data types, here's some nice code you can use:
fn type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>());
}
fn main() {
let no = 3;
let name = "Chris";
type_of(&no);
type_of(&name);
}
The above code will print i32
and string
when run.
Variables are immutable
Variable values can't be change by default. You might start assigning a value to a variable like so:
let account_balance = 4000;
and then later you want to change it to say 4500 and attempt to write the following code:
account_balance = 4500;
At this point, you will get an error back, that you can't do this, because it's immutable. At this point you will see just how awesome the Rust compiler is by providing you not only with the error message, but what to do about it as well:
let account_balance = 4000;
| ---------------
| |
| first assignment to `account_balance`
| help: make this binding mutable: `mut account_balance`
7 | account_balance = 4500;
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
The error message suggest we make this mutable. Let's explain what that means in the next section.
Change variable value
Variables are immutable but there are ways to change them. There are two major approaches to change their values:
-
make them mutable, this involves using the keyword
mut
when declaring the variable. Like so:
let mut account_balance = 4000;
account_balance = 4500;
- shadowing, what we do here is declare a variable later down in the code with the exact same variable name but with a new variable name. What happens is that the old variable is "shadowed over" by the name variable and can no longer be referenced to:
let account_balance = 4000;
// other code
let account_balance = 4500; // this version of account_balance shadows the first declared version.
Constants
So far we've been using the the let
keyword to declare variables. Another way to declare variables is by using the const
keyword. const
means something different than let
though, with const
you declare a variable that should never change. What you are saying in the code is that here's a variable that we don't expect to ever change like the value of PI or C for the speed of light.
To declare such a variable, we can type like so:
const PI:f32 = 3.14
Notice the usage of const
and that we explicitly need to set a type, :f32
, a 32-bit floating-point type. In this case, we choose f32
, but the default would be f64
per below compiler recommendation.
Where you to skip the type, you would get an error like so:
|
6 | const PI = 3.14;
| ^^ help: provide a type for the item: `PI: f64`
There's also another type of constant, static
, but it's outside the scope of this article, if you want to learn more about that one, check the docs.
Summary
In summary, we've learned about bit more on why variables, how to declare them and assign different types of values to them. Additionally, we've seen how variables are immutable but can be changed either by using mut
keyword or shadowing, declare a varible by the same name.
This content originally appeared on DEV Community and was authored by Chris Noring
Chris Noring | Sciencx (2021-11-19T15:19:42+00:00) Rust from the beginning, variables. Retrieved from https://www.scien.cx/2021/11/19/rust-from-the-beginning-variables/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.