Rust

From Gentoo Wiki
Jump to:navigation Jump to:search
This article is a stub. Please help out by expanding it - how to get started.

Rust is a general-purpose, multi-paradigm, compiled, programming language.

Installation

USE flags

A recommended USE flag to enable is system-llvm, but it can lead into a failed build due to possible LLVM version differences.

USE flags for dev-lang/rust Systems programming language from Mozilla

+lto Enable Link-Time Optimization (LTO) to optimize the build
big-endian Big-endian toolchain support
clippy Install clippy, Rust code linter
debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
dist Install dist tarballs (used for bootstrapping)
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
llvm-libunwind Use sys-libs/llvm-libunwind instead of sys-libs/libunwind
miri Install miri, an interpreter for Rust's mid-level intermediate representation (requires USE=nightly, sometimes is broken)
nightly Enable nightly (UNSTABLE) features (NOTE: it does not install nightly version, just enables features marked as nightly at time of release)
parallel-compiler Build a multi-threaded rustc (experimental, not tested by upstream)
profiler Build the profiler runtime and rust-demangler tool (needed for '-C profile-generate' or '-C instrument-coverage' codegen opts)
rust-analyzer Install rust-analyzer, A Rust compiler front-end for IDEs (language server)
rust-src Install rust-src, needed by developer tools and for build-std (cross)
rustfmt Install rustfmt, Rust code formatter
system-bootstrap Bootstrap using installed rust compiler
system-llvm Use the system LLVM installation
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
verify-sig Verify upstream signatures on distfiles
wasm Build support for the wasm32-unknown-unknown target

USE flags for dev-lang/rust-bin Systems programming language from Mozilla

big-endian Big-endian toolchain support
clippy Install clippy, Rust code linter
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
prefix Defines if a Gentoo Prefix offset installation is used
profiler Build the profiler runtime and rust-demangler tool (needed for '-C profile-generate' or '-C instrument-coverage' codegen opts)
rust-analyzer Install rust-analyzer, A Rust compiler front-end for IDEs (language server)
rust-src Install rust-src, needed by developer tools and for build-std (cross)
rustfmt Install rustfmt, Rust code formatter
verify-sig Verify upstream signatures on distfiles

Emerge

Tip
Rust takes a relatively long time to compile. Unless there is a specific reason not to (such as the need for non default USE flags), use the "-bin" version!

Emerge the package base:

root #emerge --ask dev-lang/rust

Alternatively, there's also a binary package for users preferring to avoid long compile times:

root #emerge --ask dev-lang/rust-bin

Distcc MAKEOPTS

Rust does not support distcc compiling and may require local package.env defined to avoid excessive I/O consumption when building on distcc tuned hosts (i.e. MAKEOPTS="-j30 -l4", 30 is greater than 4 local CPU cores).

--local-load is not honored, so an appropriate --jobs value in relation to the locally available build resources must be passed:

FILE /etc/portage/env/makeopts.conf
MAKEOPTS="-jN"
FILE /etc/portage/package.env/rust
dev-lang/rust makeopts.conf

Configuration

Environment variables

RUSTFLAGS is an environment variable that Cargo[1], the Rust package manager[2], gives rustc, the Rust compiler[3], space-separated "codegen" -C flags[4]. Here are example settings, not including cross language link time optimization[5] or model specific parallelism[6], for /etc/portage/make.conf:

FILE /etc/portage/make.conf
# target-cpu=native is the equivalent of -march=native in C/CXXFLAGS:
RUSTFLAGS="-C target-cpu=native"
# enable target-cpu=native and DT_RELR
RUSTFLAGS="-C target-cpu=native -C link-arg=-Wl,-z,pack-relative-relocs"
# opt-level is similar to Clang's optimization options
RUSTFLAGS="-C opt-level=3"

Eselect Rust

There is an eselect available to switch between different Rust slots. Usage in the usual way, get a numbered list of installed Rust versions with:

user $eselect rust list

Set the version of rust with the number x via:

user $eselect rust set x

Rustup

Rustup can be used to setup a cargo-compatible environment for Rust development. First, install the package:

root #emerge --ask dev-util/rustup

Then, use rustup-init-gentoo to symlink the system rust installation to the local user's home directory:

user $rustup-init-gentoo --symlink
'/home/larry/.cargo/bin/rustc' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rustdoc' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/cargo' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rust-lldb' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rust-gdb' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rust-gdbgui' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rls' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/cargo-clippy' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/clippy-driver' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/cargo-miri' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rust-analyzer' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rustfmt' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/cargo-fmt' -> '/usr/bin/rustup-init'
'/home/larry/.cargo/bin/rustup' -> '/usr/bin/rustup-init'
* Setting gentoo rust-1.77.1 as default toolchain
rustup 1.27.0 (2024-03-24)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
verbose: read metadata version: '12'
verbose: creating toolchains directory: '/home/larry/.rustup/toolchains'
verbose: installing toolchain 'gentoo'
verbose: toolchain directory: '/home/larry/.rustup/toolchains/gentoo'
verbose: linking directory from: '/home/larry/.rustup/toolchains/gentoo'
verbose: toolchain 'gentoo' installed
verbose: read metadata version: '12'
info: default toolchain set to 'gentoo'
Default host: x86_64-unknown-linux-gnu
rustup home:  /home/larry/.rustup

gentoo (default)
rustc 1.77.1 (7cf61ebde 2024-03-27) (gentoo)
* Prepend /home/larry/.cargo/bin to your PATH to use rustup
* rustup selfupdate is disabled, it will be updated by portage

Alternatively, you can use rustup-init to install rust separately in your home directory, isolated from the system's rust installation. This may be necessary for tools that require a local user installation, for example the esp-rs toolchain. You will need to add your local installation's rust binaries to your PATH such that they override the system's binaries, like so:

FILE ~/.config/localrust.shOverriding system rust using a script
export PATH="${HOME}/.cargo/bin:${PATH}"

It might be desirable to add this script as an alias in your .bashrc, so you can use the system installation of rust by default, and switch to your local environment by running localrust:

FILE ~/.bashrcAdding alias in bashrc
alias localrust=". ~/.config/localrust.sh"

Usage

Development

While the purpose of Gentoo's Rust package is mainly to provide a build environment to emerge other packages that use Rust, it is still possible to develop in Rust with this package. The Rust compiler, rustc, is installed and available as soon as the package is emerged. However, there are certain other things to consider when developing in Rust.

In a generic installation of Rust (one that is not done with Gentoo's Rust package) things are usually updated and installed with dev-util/rustup.

Another way is to install another Rust for development separately with the instructions from the Rust documentation.

Language servers

For code autocompletion and highlighting, two language servers are available in the Rust universe: Rust-analyzer and the Rust Language Server.

As of today RLS is deprecated and now longer maintained. But it still comes with the Rust package when the rls USE flag is enabled. Rust-analyzer was merged into the Rust language and is now part of Rust since version 1.64.0. Rust-analyzer can be installed with the enabled USE flag rust-analyzer for dev-lang/rust (or dev-lang/rust-bin).

In order for the language server to autosuggest code from the Rust standard library, emerge dev-lang/rust (or dev-lang/rust-bin) with rust-src.

Code formatting

Rust comes with its own built in code formatter. Emerge dev-lang/rust (or dev-lang/rust-bin) with rustfmt enabled to use it. Code formatting is invoked in a project directory with cargo fmt.

Code linting

Rust also comes with an officially supported code linter called clippy. In order to use it with Gentoo's dev-lang/rust (or dev-lang/rust-bin) package, emerge that package with clippy set.

Rust vs other programming languages

Rust vs. C++

Both Rust and C++ are powerful programming languages, but they have distinct features and design principles that set them apart. Rust provides a more modern and safe alternative to C++. It prioritizes memory safety through its ownership, borrowing, and lifetimes system, effectively preventing common memory-related bugs.[1] Rust's expressive syntax aims for readability and maintainability, and it offers built-in safety features that help mitigate programming mistakes. While Rust's ecosystem of libraries may not be as extensive as C++ yet, it is rapidly growing and being actively developed. Rust is gaining in popularity [2], particularly in projects that emphasize security, reliability, and parallelism.

On the other hand, C++ provides a long-standing, versatile language with extensive libraries and a mature toolchain, making it a popular choice for a wide range of applications. [3] It offers developers low-level control and direct hardware manipulation, making it suitable for resource-constrained environments and performance-critical applications. [4] Additionally, C++ benefits from a larger ecosystem and a bigger pool of experienced developers and a wealth of existing codebases, which can facilitate code reuse and finding expertise. [5]

The choice between C++ and Rust ultimately depends on the specific project requirements. If low-level control and direct hardware manipulation are essential, C++ may be the preferred choice. However, for projects where memory safety and modern language features are crucial, Rust offers a safer alternative without sacrificing performance. Factors such as the level of control needed, the importance of safety, and the availability of expertise and codebases should be considered when making a decision between these two languages.

See also

  • Application level package management — provides best practice recommendations on managing the coexistence of operating system and application level package managers on Gentoo.
  • Assembly language — the lowest level of all programming languages, typically represented as a series of CPU architecture specific mnemonics and related operands.
  • C — a programming language developed for Bell Labs in the early 1970s
  • C++
  • Clang — a C/C++/Objective-C/C++, CUDA, and RenderScript language front-end for the LLVM project
  • Forth — a heavily stack-oriented self-compiling procedural programming language that is only slightly more abstract than assembly.
  • WD-40 — a feature in Gentoo profiles to disable Rust being built on the system

References

  1. The Rust Project Developers. Environment Variables Retrieved on June 16, 2024.
  2. The Rust Project Developers. The Cargo Book Retrieved on June 16, 2024.
  3. The Rust Project Developers. The rustc Book Retrieved on June 16, 2024.
  4. The Rust Project Developers. Codegen Options Retrieved on June 16, 2024.
  5. The Rust Project Developers. Linker-plugin-based LTO Retrieved on October 28, 2024.
  6. The Rust SIMD Performance Guide Contributors. Rust SIMD Performance Guide Retrieved on October 28, 2024.