Getting Rust-y — I

Jeff C.
5 min readApr 1, 2022

In this series I’m going to tell my experience so far with Rust.

Photo by Zdeněk Macháček on Unsplash

Introduction

I’ve been a software engineer for the past 12 years professionally. Before that I went to college, so basically I’ve been around coding pretty much half of my life. I’ve seen a lot of programming languages, frameworks and technologies come and go, and my curiosity always brings me to learn a little bit about them. Currently the language everybody is talking about is Rust, so I decided to start documenting my experience with the language so far in a series of articles.

This series is not by any means a guide for learning Rust, for example I’m not going to be covering here the installation process of rust. There’s tons of tutorials that cover installation and goes into every detail. This is just my experience with Rust and some fun projects I will be doing every now and then with the language.

The Infamous Hello World

For some reason that I don’t quite understand, everybody starts with the infamous hello world program. Apparently you can’t start coding in a new language if you don’t start with hello world so, here it is, the infamous hello world in Rust.

This time we need a main function that tells rust where to start the execution of this program. With fn we declare a new function and main is the name of that function. We can run this program in different ways. We could use rustc to compile this file like this.

rustc hello-world.rs

This is going to create an executable that we can run as

./hello-world

But also we could’ve created a project with cargo and run cargo run, but I’m going to leave that for a later post, explaining cargo. modules and crates.

Also noticed in line number 2 println! has as ! at the end, this in rust is called a macro. The way I see macros in rust, is are special functions that get expanded during compile time and bring extra functionality with them. So far I’ve been using some macros but I haven’t created some macros myself. Once I have more information about writing custom macros I’ll write a post about it.

Back to the Primitives Times

In Rust we have primitive types. Those primitive types are divided into two big groups, scalar and compound. Let’s start talking about scalars.

Scalars are composed by integers, floats, boolean, and character. Booleans are the easiest one to explain, they’re either true or false. Integers we have subdivisions signed and unsigned and how many bits the integer is going to have. For example we can have the following for signed and unsigned:

  1. i8 | u8
  2. i16 | u16
  3. i32 | u32
  4. i64 | u64
  5. i128| u128

For floats we only have two: f32 and f64. All characters have 4 bytes and you can even do this:

let my_emoji = ‘👀’;

Finally, one thing I have to mention about scalars and variables in general, everything is immutable by default. So declaring something like this:

let my_age = 29;

my_age is going to be immutable, meaning you can not change it. In order to change it we need to explicitly tell rust that the variable is going to be mutable like this:

let mut my_age = 29;
my_age += 3;

Now we have the compound types, those are tuples and arrays. Which we are going to discuss next.

Arrays meh! Vectors? yeh yeh yeh!

So over here we have tuples and we have arrays as compound types. Tuples are just finite holders that can have different data types. For example a simple tuple would be something like:

let jeff = (“Jeff”, 35, ‘C’);

In order to access the elements inside, rust does something clever. We use a . notation(like object in other languages) but with the index of the element of the tuple.

println!(“{}”, jeff.0); // This would print “Jeff” to the console

So using tuples it’s like a combination of objects/arrays. Now let’s talk about arrays. Arrays are also finite holders but contrary to tuples they can only be one data type at a time.

let my_array = [1,2,3,4]; // This would create an array of 4, i32 elements.

Arrays can’t be resized, this means that arrays are static. Once you create the array with a certain length you cannot change it. On the other hand if you want to mutate the elements of the array you should explicitly tell rust that you want the array to be mutable like:

If you think with the mut keyword you can resize the array I have bad news for you, you cannot 😔 but rust gives us one solution for this, VECTORS!!!

Vector in its essence are a struct (which we are going to discuss next) but they behave like JavaScript arrays or python lists. They only can have only one data type at a time. For example:

Notice that in order to alter elements we need to declare the vector as mutable, if we don’t do that the compiler is going to give us an error. There’s another way we can initialize vectors in rust and that’s using the vec! macro.

This syntax is definitely shorter and my preferred way to declare a vector. Noticed again in order to use the push method we need to declare the vector as mutable. There’s other methods available for vectors I suggest you go to check Rust std library api documentation to check them out.

Structs and Enums

So Rust has structs and enums just like C/C++ but they are a little bit different. This is the way we declare “objects” in Rust. I add the quotes because they’re not objects per se but you get the idea. Let’s start with structs.

Here we created a struct called Person composed by two attributes name and age. Pretty simple. first we create the first person by initializing the struct, pretty much like a JavaScript object but with the name of the struct preceding the curly brackets. That was interesting to me first time I saw it but then it was even more interesting when I can do exactly the same thing I do in JavaScript, having variables named the same as properties and using those to initialize the object. Like whoa!! Beautiful Rust, beautiful!!

Finally we have enums, at first I saw enums and told to myself meh! they’re the same as in Java and C/C++ but oh boy how wrong I was. These enums are far more powerful than their counterparts. Let’s see a couple of examples.

Noticed that in the StagesInLife enum we can treat them like tuples, structs or the basic type of enums we know already thanks to C/C++ or Java. The magic about the tuples and structs is that those values get kind of attached to the enum so we can reuse them later in certain validations, specially in match statements (we are going to cover match statements in the next part of this series).

Conclusion

We saw a couple of simple examples on my path of learning rust. There’s still so much to cover like, match statements, for loops, while loop, if statements, functions, modules, cargo, crates and the most important concept in Rust for me ownership and borrowing. Next week I’m going to try to cover most of these concepts and a simple blockchain that I created. Let me know what you think in the comments.

Remember to follow or subscribe so you can receive my content weekly.
Also you can follow me on twitter at:
https://twitter.com/jcar787

--

--

Principal Software Engineer | Web Development Instructor | Mentor | JavaScript | Python | Rust | AWS | GCP | Music Producer | Startups