Let’s dive right in.
No, I’m not going to say Rust has variables, that’s a given. But in Rust all variables are immutable by default. This is great because I’ve been moving more and more to preferring immutable over mutable over the last few years.
You declare variables using the
mut keyword after
let variable = 5; //immutable let mut variable = 5; // mutable
So my first thought was if we wanted immutable why not use const like js?
We can… with caveats.
Constants are defined using const like in JS. But Rust treats
constants as separate from immutable variables.
const FIVE_MINUTES:u32 = 5 * 60;
Notice the u32, constants MUST be explicitly typed.
The naming convention is upper case with underscores, kinda like how I define constant in JS too.
So why have
- scope, const is valid for the whole program,
- constants are defined at compile time, not at run time, i.e. you can’t set a const using the value of a function call or something that will be calculated at runtime.
In Rust, you can declare a variable multiple times in the same scope
let name = "Michael"; let name = "Mike";
This is called shadowing and this does not compute for me. I mean the normal assignment still fails. This:
let name = "michael"; name = "Mike";
Will error, but that first one won’t. I don’t know how I feel about this. I mean I can see some benefits, like when you have those values that are calculated using themselves
let s = 2 let s = s * 2
But it still feels weird.
I read that one of the benefits is we can reuse the name and change the type, e.g. from number to string. But isn’t this more an argument for dynamic typing? I don’t know, it makes me feel uncomfortable, but I guess I’ll get used to it.
Rust is a statically and strongly typed systems programming language. statically means that all types are known at compile-time, strongly means that these types are designed to make it harder to write incorrect programs.A Gentle Introduction To Rust
Because of this, we need to know all the types of our variables at compile time.
But don’t worry, you do not always have to be explicit because rust can infer what type we want based on the value and how we use it.
You can specify the type using type annotations. We saw these when we defined const:
let guess: i32 = 42;
this will be useful for example when parsing since values can be parsed to many types.
let guess: u32 = "42".parse().expect("Not a number");
We’ll talk about expect later, but for now it’s fair to say it’s error handling thing.
There are two types of Data Types, Scalars Types and Compound Types
Scalars contain single values and there are 4 primary ones, integers, floats, booleans and characters.
Compounds contain multiple values and there are two primaries, tuples and arrays.
let number: i32 = 4;<br>let number2: u32 = 68;
let x = 2.0; let y: f32 = 3.0;
let f = false; let t: bool = true;
characters use single quotes and represent a single character. They should not be confused with string literals which use double quotes.
let c = 'z'; let z: char = 'Z';
Characters are Unicode values so can be used for other languages.
This is new and doesn’t exist natively in JS at the time of writing and represents a grouping of a number of values. Like an array only it can not grow or shrink.
let tuple: (i32, bool, f32) = (42, true, 4.5);
you can destructure tuples, just like you can do with arrays in JS
let (x, y, z) = tuple;
And this is fun, you can access each member using the index and a dot
let x = tuple.0; let y: bool = tuple.1;
An empty tuple
() is a special case called a unit. Expressions implicitly return the unit if they do not explicitly return something else.
Unlike JS each element must have the same type, which should not be a problem for me since I rarely use that part of JS. Also unlike JS, arrays have a fixed length.
let a = [1,2,3,4,5]; let s: [i32; 3] =[1,2,3];
Here’s fun, you can initialize an array with the same value,
let a = [3;5]; // [3,3,3,3,3];
Note: when defining types for arrays remember that there is a semi-colon there
let arr: [f32;10]..., I kept making the mistake of using a comma.
You can access elements the same way as in JS
let x = arr;
And that’s it for today. That was a very brief introduction but enough to get me started. There are things I left out and others I plan on writing about at a later date. Did you learn anything? Let me know on Twitter, @phoexer, and happy coding.