DEV Community

Cover image for Fastest Intro To Rust: From Zero To Hello World
raddevus
raddevus

Posted on

Fastest Intro To Rust: From Zero To Hello World

Why Should You Learn Rust?

There are numerous reasons to learn Rust.

  • It's the easiest compiler / toolchain to install (as we'll see below) which will allow you to build native executables.

What's a native executable? It's a program that runs directly on the target processor, without any virtual machine or interpreter. This is very cool because if you want to share the executable with someone else running the same OS as you then you can just give them the exe -- no need for them to install massive libraries or interpreters or some runtime (like Java runtime, Python interpreter or .NET CLR)

  • You'll learn programming that is "closer to the metal"
    Similar to what I was saying about the native executable -- your code is compiled to run natively on the processor (there is no runtime environment or interpreter). This means you learn more about the true environment / Operating System that you are running on.

  • Overall Understanding of Programming Will Grow - You'll learn core concepts of programming. With Rust you'll learn concepts which are at the "beginning" of programming. Instead of focusing on extremely high-level things like building User Interfaces you'll learn how computers really work as you learn concepts in Rust.

  • Rust is fun - Learning to program with Rust is in many ways a simplified programming because you aren't immediately thrown into learning how to create graphical User Interfaces. This removes the burden of being overwhelmed with learning all those extras and makes programming a lot more fun. Learning to program using Rust is similar to how traditional computer programming was learned -- For example, if you've ever read the book, The C Programming Language by Kernighan and Ritchie (K&R C) you'll learn Rust in small code snippets and you'll learn each concept and build on the next. It's a great and fun way to learn to program. But, Rust is a lot more fun to learn than C, because getting everything ready to start writing Rust programs is much easier than getting the C toolchain installed.

Now, let's install Rust.

Install Rust

Windows Install

Install Rustup which will install everything you need and make it easy to update later.

Download the rustup tool and run it:
https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

Linux or macOS

Run curl to retrieve rustup:
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

Additional Install Help

For more details check out the official Rust site on installation.

Rust Is Installed

if you followed the steps on your system then rust is installed.

Now you can open a terminal (aka console) and run the following command:
rustc --version

Here's what I see on my Windows 10 machine:

rustc --version

Let's Write Our First Program

  1. Open a console or terminal window (the one you used to run the rustc program will work if you still have it open).
  2. Make a directory to hold your Rust projects. I like a directory under my /home// named dev and then I create a separate folder for each language like: /home/<user>/dev/rust or /home/<user>/dev/c# Of course on Windows that would look like %userprofile%\dev\rust which expands to c:\users\<user>\dev\rust
  3. once the make the main rust directory to hold your projects, then go ahead and create a new directory for our first project: On linux $ mkdir ~/dev/rust/hello or on Windows mkdir %userprofile%\dev\rust\hello will create the directory where we want to create our little program.
  4. Now, just change directory into that directory : Linux cd ~/dev/rust/hello or on Windows cd %userprofile%\dev\rust\hello
  5. create a new Rust source file: On Linux (use the nano text editor) type nano hello.rs or on Windows use notepad notepad hello.rs
  6. type in the following program and save it (in nano you just type Ctrl-X (exit) and then type a 'y' to confirm that you want to save the file.
fn main() {
    println!("Hello, Rust-world!");
}

Enter fullscreen mode Exit fullscreen mode

Compile the Program

Now for the good part. Just run the rust compiler (rustc)
$ rustc hello.rs It's the same on Windows

This will compile the program into an executable with the name hello (or hello.exe on Windows)

Run The Program

$ ./hello <ENTER> or on Windows \>hello <ENTER>

You will see the output.

hello, rust-world

You've done it. You've taken your first step to becoming a Rustacean.

To continue down the Rust path, check out the official documentation and follow along.

Top comments (0)