Rust on Arch Linux: Getting started

Table of contents

Summary Summary

Rust (opens in a new tab) (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 (opens in a new tab) paradigm, ownership (opens in a new tab) and zero cost abstractions (opens in a new tab) (!). 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 (opens in a new tab) and also a toolchains (opens in a new tab) (multiple release channels: stable, beta and nightly) installer called rustup (opens in a new tab).

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 (opens in a new tab) of Rust. It consists of three layers.

TierDescription
1“guaranteed to work” (with automated testing in building official binary)
2“guaranteed to build” (with automated builds and partial automated testing)
3may 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 (opens in a new tab) based on Arch Linux (opens in a new tab).

Environment Environment

Tutorial Tutorial

doas (opens in a new tab) at each can be replaced with sudo (opens in a new tab).

Installation Installation

You have at least two options even when you use pacman (opens in a new tab).

  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) Install Rust directly (Option 1)

console
$ doas pacman -Sy rust

Install rustup (Option 2) 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 function.

Create a project 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

Run cargo init (opens in a new tab) 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 (opens in a new tab).

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

Run a demo app 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 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 References

More on: Rust and WebAssembly

Scqr [secureal]

Scqr [secureal]: the engineering practice of Reflerd Inc. (opens in a new tab)

Comments and feedback are welcome.