Today I dig into Rust variables and data types. A lot of the stuff in Rust is similar to what you find in JavaScript but some of it is different in a weird way. Not bad, weird.
Let’s dive right in.
Variables
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 let
keyword, similar to javascript. But unlike javascript, let by default does what const does. If you want it to be mutable or changing then you have to use mut
keyword after let
.
let variable = 5; //immutable
let mut variable = 5; // mutable
Code language: JavaScript (javascript)
So my first thought was if we wanted immutable why not use const like js?
We can… with caveats.
Constants
Constants are defined using const like in JS. But Rust treats constants
as separate from immutable variables.
const FIVE_MINUTES:u32 = 5 * 60;
Code language: JavaScript (javascript)
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 let
without mut
?
- scope, const is valid for the whole program,
let
is scoped - 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.
Here’s weird.
In Rust, you can declare a variable multiple times in the same scope
let name = "Michael";
let name = "Mike";
Code language: JavaScript (javascript)
This is called shadowing and this does not compute for me. I mean the normal assignment still fails. This:
let name = "michael";
name = "Mike";
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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.
Types
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;
Code language: JavaScript (javascript)
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");
Code language: JavaScript (javascript)
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.
Integer
let number: i32 = 4;
let number2: u32 = 68;
Code language: JavaScript (javascript)
Floating Points
let x = 2.0;
let y: f32 = 3.0;
Code language: JavaScript (javascript)
Boolean
let f = false;
let t: bool = true;
Code language: JavaScript (javascript)
Characters
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';
Code language: JavaScript (javascript)
Characters are Unicode values so can be used for other languages.
Tuples
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);
Code language: JavaScript (javascript)
you can destructure tuples, just like you can do with arrays in JS
let (x, y, z) = tuple;
Code language: JavaScript (javascript)
And this is fun, you can access each member using the index and a dot
let x = tuple.0;
let y: bool = tuple.1;
Code language: JavaScript (javascript)
An empty tuple()
is a special case called a unit. Expressions implicitly return the unit if they do not explicitly return something else.
Arrays
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];
Code language: JavaScript (javascript)
Here’s fun, you can initialize an array with the same value,
let a = [3;5]; // [3,3,3,3,3];
Code language: JavaScript (javascript)
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[0];
Code language: JavaScript (javascript)
Conclusion
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.