Getting Started with Rust
Tags: rust, programming, learning, tutorial
Published: 2020-07-12 13:18:04

*This post published on my blog [before](https://aligoren.com/getting-started-with-rust/)*

-----

Hi everyone! In this post, I'll talk about [Rust programming language](https://www.rust-lang.org/).

## 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 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](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](https://doc.rust-lang.org/book/) 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](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;

```bash
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;

```bash
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**

```bash
rustup update
```

**Uninstall**

```bash
rustup self uninstall
```

To check Rust installed correctly use this command;

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

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

And run this command;

```main
rustc main.rs
```

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

```bash
./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**.

```bash
cargo new hello_world

cd hello_world
```

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

```tree
src/
	main.rs
Cargo.toml
```

`cargo` also inits an empty git repository.

You can run your code using this command;

```bash
cargo run
```

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

```bash
cargo build --release
```

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

```toml
[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;

```rust
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 it.

The **main** function is a special function in some programming languages. This is an 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;

```rust
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;

```rust
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

- [Official Rust Book](https://doc.rust-lang.org/book/)