in English, Programming

Getting Started with Rust

Hi everyone! In this post I’ll talk about Rust programming language.

Rust Language

Introduction

The Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.

Source: https://doc.rust-lang.org/book/ch00-00-introduction.html#introduction

You can build system software, web applications, etc. If you used any programming language before, the first steps aren’t hard to understand what is happening.

In this post, I’ll use The Rust Programming Language documentation.

In this chapter, we’ll see installation processes, our first program and cargo.

Installation

I prefer rustup to install Rust language. Actually, they recommended to rustup. I used it.

We’ll go to the Install Rust page: https://www.rust-lang.org/tools/install

You will see installation instructions for your operating system.

You can install Rust on Linux or macOS with this command;

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

You should see this message: Rust is installed now. Great!.

You can add Rust to the system path manually by this way;

source $HOME/.cargo/env

If you’re using Windows, you can find Rust’s Windows binary file on the installation page.

You can update rustup or uninstall it with these commands;

Update

rustup update

Uninstall

rustup self uninstall

To check Rust installed correctly use this command;

rustc --version

Our First Rust Program

We can write our first program in different ways. The first way is so simple. The first program will be a hello world program in both ways.

First Way

Create a file called main.rs wherever you want.

fn main() {
    println!("Hello World!");
}

And run this command;

rustc main.rs

It will create an executable file with the source code’s file name. You can run it as an executable file.

./main

Second Way

We can create a project with cargo. This cargo is a package manager for Rust. Yes you can build your project with cargo.

cargo new hello_world

cd hello_world

By this way, cargo created a project structure for us. Now our project structure is like that;

src/
    main.rs
Cargo.toml

cargo also inits an empty git repository.

You can run your code using this command;

cargo run

This command will run your main.rs file under the src folder. You can also build your project for release.

cargo build --release

You can check your Cargo.toml file. It should look like that;

[package]
name = "hello_world"
version = "0.1.0"
authors = ["Ali GOREN <YOUR_VERY_HIDDEN_EMAIL>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Let’s Review Our First Program

First of all we’ll be review this code piece;

fn main() {

}

So, really what the heck is that?

You define a function with these kinds of lines. If you’re familiar with C like programming languages, you will understand what is this. If you don’t know what is this, don’t worry. I’ll explain.

The main function is a special function in some programming languages. This is entry point to run your programs. It is always the first code that runs in every executable Rust program.

We didn’t pass parameters to this function. If you need to pass parameters, they should be inside parentheses. (params1, params2).

The second thing you should know is the curly brackets. These are wraps the function body. For example;

fn main() {
   // this is function body
}

There are some advisories to style guide but you shouldn’t know that for now.

Let’s dig into the function body.

We see this code piece in the body;

println!("Hello World!");

The println! calls a Rust macro. We’ll not compare macros and function in this post. But they’re different. For now, you should know ! means that you’re calling a macro.

Every string statement needs to be in double quotes. So, "Hello World!" is a string statement. It will be printed on the screen.

We added a semi-colon to the end of the println!. You don’t have to do that. This means this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.

That’s all. Thanks for reading.

Resources