Rust on Arch Linux: Getting started
Tags: rust, rustup, cargo, archlinux
Published: 2023-01-06 16:14:17

## Summary

[Rust](https://www.rust-lang.org/) (or rustlang) is one of modern programming languages for general purposes, which is fast, safe and productive for development.

Rust has a lot of features such as [functional programming](https://doc.rust-lang.org/book/ch13-00-functional-features.html) paradigm, [ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html) and [zero cost abstractions](https://doc.rust-lang.org/stable/embedded-book/static-guarantees/zero-cost-abstractions.html) (!). As to speed and safety, it doesn't have GC, garbage collection, so it runs with much smaller memory and cleanly. As to productivity, it has a nice package manager called [cargo](https://doc.rust-lang.org/cargo/) and also a [toolchains](https://rust-lang.github.io/rustup/concepts/toolchains.html) (multiple release channels: stable, beta and nightly) installer called [rustup](https://rustup.rs/).

It supports cross-platform so a single codebase can be compiled to be compatible with various platforms. Moreover, the output is a single binary and the size is optimized and therefore small, which provides another feature with Rust, portability.
The supported platforms are Windows, Mac, Linux (Tier 1). Not only them but also FreeBSD (Tier 2) and OpenBSD (Tier 3).

What are tiers ? They are about the [platform support model](https://doc.rust-lang.org/rustc/platform-support.html) of Rust. It consists of three layers.

| Tier | Description |
| ----- | ----------- |
| 1 | "guaranteed to work" (with automated testing in building official binary) |
| 2 | "guaranteed to build" (with automated builds and partial automated testing) |
| 3 | may or may not work (with codebase support but without automated builds and testing) |

64-bit Linux (kernel 3.2+, glibc 2.17+) is Tier 1, the most familiar with !!

This post shows how to install Rust on [Artix Linux](https://artixlinux.org/) based on [Arch Linux](https://archlinux.org/).

### Environment

- OS: Artix Linux (based on Arch Linux)
- App: Rust [1.66.0](https://releases.rs/docs/1.66.0/)
- App toolchains installer: rustup 1.25.1
- Terminal: [Xfce4 Terminal](https://gitlab.xfce.org/apps/xfce4-terminal)
- IDE (& Editor): [VSCodium](https://vscodium.com/) (or [VS Code](https://code.visualstudio.com/))

## Tutorial

[`doas`](https://man.openbsd.org/doas) at each can be replaced with [`sudo`](https://www.sudo.ws/).

### Installation

You have at least two options even when you use [pacman](https://wiki.archlinux.org/title/Pacman).

1. Install Rust directly.
2. Install rustup and get the environment of its stable channel.

The latter is officially recommended. Well, it's not essential. You can take the former as trial to take a shortcut.

Here are both examples. Open your terminal. Here we go.

#### Install Rust directly (Option 1)

```console
$ doas pacman -Sy rust
```

#### Install rustup (Option 2)

```console
$ doas pacman -Sy rustup
```

Then run:

```console
$ rustup default stable
```

The output was:

```console
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2022-12-15, rust version 1.66.0 (69f9c33d7 2022-12-12)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 68.0 MiB /  68.0 MiB (100 %)  22.9 MiB/s in  2s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.0 MiB /  19.0 MiB (100 %)  15.3 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
 29.7 MiB /  29.7 MiB (100 %)  17.4 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 68.0 MiB /  68.0 MiB (100 %)  19.7 MiB/s in  3s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.66.0 (69f9c33d7 2022-12-12)

```

Done 😉

Besides, with rustup, you can take nightly or beta version instead of stable, which possibly has shorter compilation time or richer (but perhaps in testing) function.

### Create a project

First of all, create a directory.

```console
$ mkdir my-first-rust-prj

$ cd my-first-rust-prj
```

Next, start VSCodium (or VS Code) in the directory.
Then press `Ctrl + Shift + @` with your keyboard to open terminal.

![init in ide](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hilw4mnsprj6dl50dzje.png)

Run [`cargo init`](https://doc.rust-lang.org/cargo/commands/cargo-init.html) to create a cargo package:

```console
$ cargo init --bin .
```

Besides, `--bin` option is the default and therefore may be omitted.

The output was:

```
     Created binary (application) package
```

You will see `Cargo.toml`, the project manifest file, `Cargo.lock`, the packages lock file to its dependencies, and `src/main.rs`, the program entry and so on. Additionally, `target` directory will be created after [`cargo build`](https://doc.rust-lang.org/cargo/commands/cargo-build.html).

```
.
├─.gitignore
├─src
├───main.rs
├─Cargo.lock
└─Cargo.toml
```

### Run a demo app

You got a set which is able to be compiled.

How to compile it and run as app ? Just run:

```console
$ cargo run
```

The output was:

```console
   Compiling my-first-rust-prj v0.1.0 (/(...)/my-first-rust-prj)
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/my-first-rust-prj`
Hello, world!
```

Yay. It said "Hello, world!" Brilliant :)

Alternatively, you may get the binary and execute it manually.

```console
$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

$ ./target/debug/my-first-rust-prj
Hello, world!
```

## Conclusion

Now you have an example Rust project and are ready for development.
You can manipulate data, implement File I/O, handle network connection, build a server, etc.

Enjoy your journey(s) with Rust -- the safe, the powerful, the interesting, the robust and the charming.
Bon voyage ;)

### References

- [The Rust Programming Language](https://doc.rust-lang.org/stable/book)
  - Really a great book !!
- [Learn Rust](https://www.rust-lang.org/learn)