Rust For The Scaredy Cat : Variables And Data Types
Greetings nerds 👏 welcome back to the series, Today we’ll start by setting up our dev environment and doing very basic stuff with variables and their various data types and what not
PREPARING OUR DEVELOPMENT ENVIRONMENT
Once you’ve Rust installed, go ahead and download a text editor ( honestly i started with nano on Ubuntu) like vscode or sublime text
Personally i use vscode with the rust-analyzer extension and that’s more than enough for me to get going
CREATING YOUR FIRST RUST FILE
To create a new Rust file, simply create a file ending with .rs
and open it with your favorite text editor of choice and we’ll need to add some boilerplate first
fn main() { println!("hello Rust!");
}
So if you’re wondering what the 🦆 is going on, let’s gently explain the stuff above
fn
is a reserved keyword to declare a function, a function is a block of code we can call as much as we want, it’s indicated by a pair of those curly braces
main
is the name of the function, this function is special because it tells the compiler where the program begins so yes we can’t name our functions that
println!()
is called a “macro” but for the sake of our knowledge level now, it’s fairly safe to call it a function, we use it to print to the console
ALL statements have to end in a semicolon so please get used to that
VARIABLES IN RUST
Variables in Rust can be declared using the let
keyword, so to declare a variable named age
and assign a value to it right away we can say
let age = 24;
and now we have our variable
Let’s print it out so we can see some println!()
magic using placeholders
println!("hello, i am {} years old", age); // hello, i am 24 years old
To print out a value to the standard output in Rust, We MUST place those curly braces in order for them to be substituted with the value in the second parameter of the println!()
macro
There’s a catch about variables in Rust, Variables ARE IMMUTABLE BY DEFAULT meaning once you’ve assigned a value, you can’t reassign another one later, this following example will not compile and I’ll show you how to get around it
fn main() {
let age = 24;
println!("hello i am {} years old", age);
age = 25;
println!("{}", age);
}
Now if you try to compile it using rustc
like so rustc index.rs
you’re gonna see this following error message
As stated above, we’re assigning to a variable again, the compiler also told us how to fix the issue ♥️ so let’s add this keyword mut
after let
like so let mut age
and now the program will compile
DIFFERENT DATA TYPES
Although rustc
infers the data type for variables at compile time, it’s highly recommended and is a good practice to annotate them, but first let’s discover the supported data types and the more common ones we’ll use
INTEGER TYPES
It depends on the size of the data you’re store what data type to use, but generally speaking i16
seems to be enough and you can always choose more
FLOATING POINT TYPES
There are only two available floating point types which are f32
and f64
BOOLEAN TYPE
A boolean type indicates a value is either true
or false
and it’s common in other programming languages
CHAR TYPE
This type represents a single character, i.e. let grade = 'B’
; and it has to be between single quotation marks
STRING TYPE
A string is a series of bits stored on the heap, in Rust you’ll find both String
and a data type called a string slice which is basically a reference to a string, we’ll learn how to declare both very soon
ARRAYS
Just like any other programming language, an array is a collection of items in square brackets [] separated by commas, And they have to be of the same data type when we add an integer the rest of the items must be all integers and so on
TUPLES
A tuple is similar to an array but we use those () braces instead of the square braces
Assigning a data type to our variables
Now we will once again declare our variables but we’re gonna explicitly annotate their types
let username: String = String::from("bob123"); // declaring a string
let age: i16 = 24;
let is_married: bool = false;
let my_array: [u8; 5] = [1,2,3,4,5]; // first is the data type and second is how many items
let my_typle = ("saul", "kim", "charles"); // yes i love that show
And just like that we explicitly assigned data types to our variables which will come handy ( and become required ) later on
How about some math ?
Let’s do some basic math operation using the very minimum knowledge we have now shall we
fn main() {
println!("{}", 5 + 3); // addition
println!("{}", 5 - 3); // subtration
println!("{}", 5 * 3); // mutiplication
println!("{}", 5 / 3); // division
println!("{}", 5 % 3); // the remainder of division
println!("{}", 5_i32.pow(3)); // to the power
}
I believe this is enough to know for now regarding data types in Rust and should have you comfortable with the syntax and what not, we’ll revisit more of those types so soon ( arrays and tuples to be exact ) and we’ll learn about more types so stay tuned please for the rest of the series
For further reading i highly recommend this chapter from the Rust Programming Language book https://doc.rust-lang.org/book/ch03-02-data-types.html
Happy coding !
And here’s a cutie Ferries the crab for the blog display image