Getting started with Rust
Tags: rust, beginners
Published: 2020-05-27 13:02:56

Rust is a language which I have been looking into lately and starting with this post I will begin a new series of articles to share what I have learned so far to help other beginners or those interested in getting to know rust get to grips with the language.

So what is rust? Well, the rust wikipedia page states it quite well:

> Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but provides memory safety without using garbage collection. Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others.
>
> Source: [Rust Wikipedia Page](https://en.wikipedia.org/wiki/Rust_(programming_language))

## Getting setup

To get setup go to the [installation page](https://www.rust-lang.org/tools/install) of the official rust language site. Follow the instructions for your [operating system](https://en.wikipedia.org/wiki/Operating_system) of choice and once you've installed everything you should be able to run `rustc --version` in your terminal and get something like this in response:

```sh
rustc 1.39.0-nightly (eb48d6bde 2019-09-12)
```

If that command ran correctly without any errors, the setup stage is over and we are ready to get coding!

## Hello world!

Let's make a file called `Hello.rs` and write the following inside the file:

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

The `main()` function is a required function in rust, it is the function executed when a programme is run.  `println!()` is a macro function (we will look into that concept in a future article) which is roughly equivilent to `echo` in PHP, `System.Console.WriteLine()` in C# and `console.log()` in JavaScript and it's one job like these others is to write content to the terminal/console.

> **Sidenote 1:**
>
> As a heads up, `println!()` works with values other than strings a bit differently, for example if we wanted to print a number to the terminal we would have to write `println!("{}", 2);` which basically tells the `println!()` macro to write a string where the placeholder `{}` is the value `2`. 
>
> We can output multiple values this way too, for example `println!("{}, {}", 1, 2);` will output "1, 2".
> 
> We can name our values by index too, for example `println!("{1}, {0}", 1, 2);` will output "2, 1".
>
> There is also the special debug placeholder `println!("{:?}", vec![1, 2, 3]);` for vectors, structs, etc. This example would output `[1, 2, 3]` for reference but don't worry about datatypes just yet, we will look at them in detail in an upcoming article.

Moving back on track, we can go to our terminal and compile our `Hello.rs` file using the `rustc Hello.rs` command to tell the rust compiler (`rustc`) to compile the `Hello.rs` file. This will output 2 new files names `Hello.pdb` and `Hello.exe`. 

> **Sidenote 2:**
>
> We can ignore the `.pdb` file as this is just a programme database file which the `.exe` file uses to lookup debugging information. Here is a wiki page with more information on [.pdb files](https://en.wikipedia.org/wiki/Program_database)

To run our application we can now simple type `./Hello` into the terminal to run the `Hello.exe` file and you should see it output the text "Hello world" in the terminal!

## Conclusions

Rust can seem like a daunting language that is hard to learn and quite frankly it is at times but we have all had this feeling as we begin to journey into a new language, stack or role and so let us not be daunted when challenged to learn!

I hope this article has brought you some value and has given you a nice first step into getting setup, writing, compiling and executing with rust. See you in the next one.