DEV Community

Cover image for Learning Rust πŸ¦€: 01- Intro and Setup
Fady GA 😎
Fady GA 😎

Posted on • Edited on

Learning Rust πŸ¦€: 01- Intro and Setup

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 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.

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:

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!

Python Vs Rust:

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.

Pyton Rust
Mode of operation Interpreted. Translated to machine code each time the program runs. Compiled. Translated once and the translation is run each time the program runs (faster run).
Implementation Warps another implementation written in another language (Cython which is written in C, is the most popular one for Python). Doesn't wrap any other implementation.
Type System Dynamically typed. No prior type definition is required and types can change mid-execution. Strongly typed language (less bugs). Types must be defined if it can't be inferred and types can't be changed by default.
Error Stacks Often vague, unclear and intimidating. Clear with coloring, explanations, and suggestions.

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! πŸ€·β€β™‚οΈ

Rust Toolchain:

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.

Environment Setup:

I won't write a detailed tutorial on how to install Rust. The Rust official website 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.
  • If you use VS Code, The CodeLLDB debugger 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.

Hello, World!

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:

cargo new hello_world_app
Enter fullscreen mode Exit fullscreen mode

(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.

fn main() {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

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

cargo run
Enter fullscreen mode Exit fullscreen mode

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

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! πŸ‘‹

Top comments (1)

Collapse
 
abuhermass profile image
abuhermass

*SUCH A BRILLIANT CONTRIBUTION!
*

To be honest your thoughts about approaching new language by teaching it, made me so hooked to see what will be next.. I will start to read through your series and I will see what the future holds for me!
Magnificent job!!! Keep up the go writing..