Rust
Rust is a general-purpose, multi-paradigm, compiled, programming language.
Installation
USE flags
A recommended USE flag to enable is USE=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
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
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 |
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
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
There's also an binary package provided due to the 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:
/etc/portage/env/makeopts.conf
MAKEOPTS="-jN"
/etc/portage/package.env/rust
dev-lang/rust makeopts.conf
Configuration
Environment variables
- RUSTFLAGS
For example, in /etc/portage/make.conf, one may set:
/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
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 Rustup. This will not work with the Rust toolchain that is installed with Gentoo. Instead additional Rust things that would be installed by Rustup are installed via USE flags of the dev-lang/rust package.
Another way is to install another Rust for development separately with the instructions from the Rust documentation. Additionally there is the Gentoo Rust overlay with further packages for development and software written in Rust.
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 USE=rls 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 USE=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 USE=rust-src.
Code formatting
Rust comes with its own built in code formatter. Emerge dev-lang/rust (or dev-lang/rust-bin) with USE=rustfmt 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 USE=clippy.
See also
- Application level package management — provides best practice recommendations on managing the coexistence of operating system and application level package managers on Gentoo.