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
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.
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
This is called shadowing and this does not compute for me. I mean the normal assignment still fails. This:
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
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:
this will be useful for example when parsing since values can be parsed to many types.
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.
characters use single quotes and represent a single character. They should not be confused with string literals which use double quotes.
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.
you can destructure tuples, just like you can do with arrays in JS
And this is fun, you can access each member using the index and a dot
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.
Here’s fun, you can initialize an array with the same value,
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
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.