> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Rust Basics Series #1: Create and Run Your First Rust Program
- URL: https://itsfoss.com/rust-introduction/
- Published: 2023-03-28T06:00:31.000Z
- Updated: 2024-01-18T12:44:51.000Z
- Description: In the first chapter of the Rust programming series, you learn to write and execute your first program in Rust.
- Author: Pratham Patel
- Tags: Rust Basics 🦀, #docs

So, you've decided to try coding in Rust. 

And why not? After all, it is one of the fastest adopted systems programming languages by developers and tech companies. It is also the only language other than C that is used in Linux kernel code.

In the first part of the Rust Basics series, you'll learn to write your first ever Rust program.

Before you do that, you must install it first. Let's go on with that.

## Installing the Rust compiler

I would prefer that you have the [Rust compiler installed locally](https://itsfoss.com/install-rust-cargo-ubuntu-linux/). You can do so by running the following command:

```bash
curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh
```

![Installing Rust on Ubuntu Linux](https://itsfoss.com/content/images/2023/03/install-rust.svg)

Installing Rust

Apart from the Rust Compiler, I also recommend installing a few more tools that will help you in the development process:

```bash
rustup component add rust-src rust-analyzer rust-analysis

```

You also need to [install gcc](https://learnubuntu.com/install-gcc/?ref=itsfoss.com). Otherwise, you may encounter `linker 'cc' not found` error. The package is called gcc across distributions. 

On Ubuntu and Debian use: 

```
sudo apt install gcc
```

💡

If you do not wish to install the Rust compiler, no worries. You can run the Rust code directly in your browser! Just head over to the [Rust Playgrounds website](https://play.rust-lang.org/?ref=itsfoss.com) and paste the code discussed here.

## Hello Rust!

Since Dennis Ritchie and Brian Kernighan introduced the C programming language with the "Hello world" program, it has become a custom in the UNIX world to do so with any new programming language you learn.

So let's write our Hello World program in Rust as well.

I will [create a project directory](https://itsfoss.com/make-directories/) called `learn-rust-its-foss` in my home directory. In there, I create another directory called `hello-world`. Inside that, I will create a `main.rs` file:

```
// this code outputs the text
// "Hello world!" to `stdout`

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

```

📋

Just like C, C++ and Java source files have the extensions .c, .cpp and .java respectively, the Rust source files have the .rs file extension.

As a C/C++ programmer, you might have used [gcc on Linux](https://learnubuntu.com/install-gcc/?ref=itsfoss.com), `clang` on macOS and MSVC on Windows. But to compile Rust code, the language creators themselves provide an official `rustc` compiler. 

Running a Rust program is the same as [executing C/C++ program](https://itsfoss.com/run-c-program-linux/). You compile the code to get the executable file and then run this executable to run the code.

```bash
$ ls
main.rs

$ rustc main.rs

$ ls
main  main.rs

$ ./main
Hello world!

```

Nice!

## Deciphering Rust code

Now that you wrote, compiled and ran your first ever Rust program, let's de-structure the "Hello world" code and understand each part.

```rust
fn main() {
}

```

The `fn` keyword is used to declare a function in Rust. Following it, `main` is the name of this particular function that was declared. Like many compiled programming languages, the `main` is a special function used as your program's entry point.

Any code written inside the `main` function (between the curly brackets `{` `}`) gets executed upon program start-up.

### println macro

Inside the `main` function, there is one statement:

```rust
    println!("Hello world!");

```

Like the C language's standard library has the `printf` function, Rust language's standard library has the `println` **macro**. A macro is similar to a function but it is distinguished by the **exclamation mark**. You'll learn about macros and functions later in this series.

The `println` macro takes in a format string and puts it to the program's output (in our case, that is terminal). Since I wish to output some text instead of a variable, I will enclose the text inside double quotes (`"`). Finally, I end this statement using a semi-colon to denote the end of the statement.

📋

Just know that anything that looks like a function call but has an exclamation mark (!) before the opening parentheses is a macro in the Rust programming language.

### Commenting

Rust follows the known commenting style of the C programming language. A single line comment starts with two forward slashes (`//`) and a multi-line comment is started by `/*` and ends with `*/`.

```rust
// this is a single-line comment
// but nothing stops me from doing the same
// on the second or third line too!

/*
 * this is a "true" mutli-line comment
 * because it is _fancy_
 */

```

## Conclusion

You just took the first step towards coding in Rust with the Hello World program. 

As a practice, perhaps you can write and execute a Rust program that prints "Yes! I did Rust".

In the next part of the series, you'll learn to use variables in your Rust program. Stay tuned!