Learning Rust 🦀: 01- Intro and Setup
Tags: learning, rust
Published: 2023-06-03 11:24:38

## About Me:
Hi! My name is Fady. I'm a Telecommunications engineer transitioning into the world of Data and the Cloud. I'm not a professional developer but I use Python A LOT. Over the years, I've picked up a thing or two about Python and I use it mostly for Data Engineering stuff. I like learning new things (not just tech stuff!) ... it keeps my days fresh and interesting 😉

## My Motivation to write this Rust series:
Like a lot of you, I've heard the buzz around Rust-lang these days and how it promises "lightning-fast performance" and "safety". I began to see movements to popularize Rust in the Data field and other movements that take a shot at Python's throne over the Data and AI domains! Which seems logical if Rust's promises proved to be true! So, I've decided to learn Rust! But this is not my motivation to "write" a series about it!  
When I started, I thought to myself, "Wouldn't it be great if I had someone to tell him/her about my Rust progress and what I've learned?" and at the exact same day while scrolling LinkedIn, a post from [Du'An Lightfoot](https://www.linkedin.com/in/duanlightfoot/) just hit me:
>You want to learn a complicated topic in depth?
>TEACH IT!

I considered that as a sign! 🕯️  
Rust is a new topic to me and it's somewhat complicated and I want to learn it in depth, So I'll teach it! 🙂

## The Format and Audience of this Series:
I will follow loosely the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols which I'm currently reading to learn Rust (It's a great book by the way!).  
I will try to write each article of this series from the perspective of an intermediate Python developer so this series won't be suitable for the absolute programming beginners.  
As I'm writing this primarily to aid me in my Rust learning, any feedback (or corrections) is very welcome!
I'll do my best to write a new article each week. I'll post new articles updates on [my LinkedIn](https://www.linkedin.com/in/fady-abdelmassih).

### Enough about giving the context and let's dive in! Future articles will be "less intro" and "more code" ... I promise!

### Table of contents:
- [Rust promises.](#rust_promises)
- [Python Vs Rust.](#python_vs_rust)
- [Rust toolchain.](#rust_toolchain)
- [Environment Setup.](#environment_setup)
- [Hello, World!](#hello_world)

<h3 id="rust_promises">Rust Promises:</h3>
Almost every Rust introductory article out there speaks of two main Rust features, "fast performance" and "Memory safety".  
The first one is self-explanatory; it is fast due to the fact that it is a low-ish level programming language that is closer to the computer hardware than other languages without intermediary layers (its similarity to C and C++ is almost always brought up). And it's complied, meaning it gets translated to "machine code" once (and if passes without errors) this optimized fast code will run each time. No need for interpreters in the middle like Python!  
The "Memory safety" part was new to me and I didn't get it at first because with Python, I didn't even know that it is a thing! It turned out that part of a Programming Language's job is to "manage the computer system memory". Meaning, "allocating" free memory space for the program to use and "releasing" this space when the program is done with it. Some languages handle that and the Programmer does not even bother with it (this feature maybe called "garbage collecting" which I've read can impose performance issues). Some other languages (like C and C++) don't do that and the Programmer has to allocate and release memory for his program which (if done wrong) can lead to problems! Imagine this, you have allocated some memory space for your program but released it too soon, problem! Or released it not soon enough, data corruption!  
Rust handles that with its "ownership system" (which I'll talk about in future articles) that ensures memory safety all the time!

<h3 id="python_vs_rust">Python Vs Rust:</h3>
Don't get your hopes up! This isn't a performance comparison 😁. This is an architectural and operational comparison between the two languages based on facts.
<table>
<tr><th></th><th>Pyton</th><th>Rust</th></tr>
<tr><td>Mode of operation</td><td>Interpreted. Translated to machine code each time the program runs. </td><td>Compiled. Translated once and the translation is run each time the program runs (faster run).</td></tr>

<tr><td>Implementation<td>Warps another implementation written in another language (Cython which is written in C, is the most popular one for Python). <td>Doesn't wrap any other implementation. </tr>

<tr><td>Type System<td>Dynamically typed. No prior type definition is required and types can change mid-execution. <td>Strongly typed language (less bugs). Types must be defined if it can't be inferred and types can't be changed by default. </tr>

<tr><td>Error Stacks<td>Often vague, unclear and intimidating. <td>Clear with coloring, explanations, and suggestions. </tr>

</table>
And those are just a few of what I've learned so far. It seems that Rust has the edge in the speed department... at least on paper! 🤷‍♂️

<h3 id="rust_toolchain">Rust Toolchain:</h3>
In a standard Rust environment, you will mostly have:
- `rustup`: Rust's "toolchain manager", mainly responsible for managing, updating and, setting up Rust environments. 
- `cargo`: Rust's build system and package manager, this is basically what you will mostly use while "Rust-ing" 😁
- `rustc`: The Rust compiler which you will use indirectly if you use `cargo`.

<h3 id="environment_setup">Environment Setup:</h3>
I won't write a detailed tutorial on how to install Rust. [The Rust official website](https://www.rust-lang.org/) does a pretty good job doing that. But there are somethings I want to point out:
- You only have to follow the website to install `rustup`, all the toolchain will be installed for you and you will be good to go.
- If you are on Windows, you can use WSL to install Rust and use Vs code to connect to your remote session via [The WSL Vs Code Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl).
- If you use VS Code, The [CodeLLDB debugger](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb) will make your life a LOT easier!
- It's a bit tricky to install Rust on Windows without using WSL but it's doable if you want to build "exe" binaries.

<h3 id="hello_world">Hello, World!</h3>
As I've mentioned, you will mostly use `cargo` during the development of a Rust application. `cargo` has a lot of commands (type `cargo --help` to list them) but initially I'd like for you to know the basic ones:  
- `cargo new <project_name>`: This is to create a new project in the current directory.
- `cargo check`: Checks the current project for errors, but doesn't build it (fast checks).
- `cargo build`: Checks and builds the project (doesn't run it).
- `cargo run`: Checks, builds and runs the project.  

So our first order of business is to create a new Rust hello world project:
```bash
cargo new hello_world_app
```
(Rust is using snake_case naming convention just like python)  
This will create a project directory called `hello_world_app` in the current directory which contains our project structure and a git repo. Our code will be in the `src` directory and in the `main.rs` file which is the entry point to our app. The `main.rs` is filled already with our "Hello, World!" example.
```rust
fn main() {
    println!("Hello, world!");
}
```
Which looks simple enough, except for the `println!` which looks like a function but it's a "macro" (more about Rust macros later). Rust uses curly-braces "***{}***" like Java, C and C++ to define a block of code (or scopes as we will see later). Every line must be terminated with a semi-colon "***;***" (except for the return value for functions, more on that when we talk about functions).  
To run our app, just type
```bash
cargo run
```
You will notice that your app is first compiled (and built), then run! Type `cargo run` again and you will see it will only run without being built (unless you make a code change).  
Also, a `target` directory will be created containing the build files (currently in the `debug` directory, but more on that later) which contains your app binary file.  

![Demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bh1qbkrl65yjbmzqbmm.gif)  

Now that you had a taste of Rust-Lang, I hope you are as excited as I'm to learn Rust with me! See you in another article! 👋