Rust

Organisation:Copyright (C) 2024-2024 Olivier Boudeville
Contact:about (dash) howtos (at) esperide (dot) com
Creation date:Tuesday, March 12, 2024
Lastly updated:Saturday, April 13, 2024

Overview

Rust is a multi-paradigm, general-purpose, efficient, safe language available as free software; see its official website for more details.

Documentation

One should read the official Rust book (The Rust Programming Language), which is clear and well written.

See also:

Installation

Even if done here in the context of Arch Linux, for the procedure that we recommend the actual distribution does not matter.

As we intend to develop (rather than just running or installing Rust software), we prefer relying on the Rustup toolchain manager, so that multiple toolchains, for multiple platforms and architectures, can be used.

Rather than using pacman to install its rustup package, we prefer going the more standard Rust way. One should follow the official Rust guidelines for that; here is our corresponding procedure:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > install-rustup.sh
# Inspect install-rustup.sh before running it.
$ sh install-rustup.sh

From now the rustc compiler and the Cargo package manager are available on the system, from one's account (in the ~/.rustup and ~/.cargo trees respectively). All Rust-related commands are then be available from ~/.cargo/bin.

One may prefer customising the installation and avoiding that one's shell configuration is automatically modified; then it is just a matter of editing typically one's ~/.bashrc or related by oneself and adding there export PATH="${HOME}/.cargo/bin:${PATH}" (another option is to add . ${HOME}/.cargo/env instead).

Then updating the current shell accordingly (e.g. . ~/.bashrc) allows to check that for example rustc can now be executed as intended.

As Rust does not perform the linking by itself, a linker (typically provided by gcc) must be available.

Afterwards Rust may be updated thanks to rustup update.

Should some day Rust and rustup have to be uninstalled, run rustup self uninstall.

Examples

Most Basic One

In ~/hello.rs:

fn main() {
   println!("Hello from Ceylan-HOWTOs!");
}

One may notice the use of a macro (! suffix), and that most lines of Rust code end with a semicolon (;).

To be compiled and linked with rustc hello.rs and run with ./hello; yields as expected: Hello from Ceylan-HOWTOs!.

This executable occupies 3.7MB (!); more information:

$ file hello
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2,
BuildID[sha1]=c77ef3f032c2f1961866e0889d54d6342a9e3554, for GNU/Linux 4.4.0, with debug_info, not stripped

No need to devise one's GNU make automatic rules - cargo will do best.

More Advanced Topics

Mode of Operation

Rust relies on LLVM to generate its code, in order to be platform and hardware-agnostic, and to achieve best performance.

Quick Facts

Language Bindings

Short Hints

Optimizing for native CPU platform

The current target platform is given by rustup toolchain list; in our case it is stable-x86_64-unknown-linux-gnu.

Then, to request Cargo to always compile code optimized for the native CPU platform, one may add in ~/.cargo/config:

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-cpu=native"]

Note that the resulting binary is expected to depend on the precise local CPU and thus should not be distributed - lest it cannot be run on other computers.

Accelerating Builds

A shared compilation cache - specifically sccache may be used to speed up builds.

See this section for more details.

Micro-Cheat Sheet

Rust Resources