Rust

From Wikipedia:

Rust is a general-purpose, multi-paradigm, compiled programming language sponsored by Mozilla Research. It is designed to be a "safe, concurrent, practical language", supporting pure-functional, imperative-procedural, and object-oriented styles. The goal of Rust is to be a good language for creating highly concurrent and highly safe systems, and programming in the large. This has led to a feature set with an emphasis on safety, control of memory layout, and concurrency. Performance of idiomatic Rust is comparable to the performance of idiomatic C++.

Core language

Rust Core Library

The Rust Core Library is the dependency-free foundation of the Rust Standard Library. It interfaces directly with LLVM primitives, which allows Rust to be platform and hardware-agnostic. It is this integration with LLVM that allows Rust to obtain greater performance than equivalent C applications compiled with Clang, making Rust software designed with libcore lower level than C. It contains only basic platform-independent types such as Option, Result, and Iterator. Developers looking to target software for embedded platforms may forego the standard library with #![no_std] to exclusively use the no-batteries-included core library for smaller binary sizes and improved performance. However, using #![no_std] limits the amount of software support that you can get from the larger Rust community as a majority of libraries require the standard library.

Rust Standard Library

The Rust Standard Library provides the convenient high level abstractions by which a majority of portable Rust software is created with. It features convenient features such as the Vec and String types; a vast amount of methods for language primitives; a large number of standard macros; I/O and multithreading support; heap allocations with Box; and many more high level features not available in the core library.

Release cycle

Rust follows a regular six-week release cycle, similar to the release cycle of Firefox. With each new release, the core and standard libraries are improved to support more platforms, improve performance, and stabilize new features for use with stable Rust.

Installation

The two main ways to install Rust are:

  • The Native installation, recommended if you only use rust for running or installing software made with Rust
  • The Rustup installation, recommended if you intend to program anything in Rust

Native installation

To install the latest stable version of Rust from the official Arch Linux software repository, install the rust package. This will install the compiler and Cargo.

There is also a development version of the Rust compiler available from the AUR. Use for prebuilt generic binaries or to build the compiler with system libraries.

Rustup

The official and recommended method of installing Rust for the purpose of developing software is to use the Rustup toolchain manager, written in Rust.

The benefit of using the Rustup toolchain manager instead of the standalone prepackaged Rust in the software repository is the ability to install multiple toolchains (stable, beta, nightly) for multiple targets (windows, mac, android) and architectures (x86, x86_64, arm).

There are two choices for a Rustup installation, one is supported by Arch Linux via pacman, while the other is officially supported by Rust via their installation script.

Arch Linux package

is available on the Arch Linux software repository. Note that rustup self update will not work when installed this way, the package needs to be updated by pacman.

This package has the advantage that the various Rust executables live in /usr/bin, instead of , removing the need to add another directory to your .

In order to install the toolchain, you need to tell rustup which version to use: or .

Example:

Upstream installation script

Rustup is also available to download and install manually via rustup's official web page.

Download the file with , view it: , and run the script ./rust.sh to start rustup installation. The script makes PATH changes only to login shell configuration files. You need to source ~/.cargo/env until you logout and login back into the system. To update rustup afterwards, run rustup self update.

The script installs and activates the default toolchain by default (the one used by the rust package), so there is no need to manually install it to start using Rust.

Usage

You might need to manually install a toolchain, e.g. , , or . You also need to do this if you want to use/test another toolchain.

$ rustup toolchain install toolchain

You can now run the Rust commands by running, . However, to use these commands directly, you need to activate the toolchain:

$ rustup default toolchain

Check the installed Rust version using :

Test your installation

Check that Rust is installed correctly by building a simple program, as follows:

~/hello.rs
 fn main() {
     println!("Hello, World!");
 }

You can compile it with , then run it:

Cross compiling

Using rustup

You can easily cross-compile using Rustup. Rustup supports many cross-compile targets. A full list can be found running rustup target list.

For instance, if you want to install rust using the stable channel for Windows, using the GNU Compiler, you will need to do:

$ rustup toolchain install stable-x86_64-pc-windows-gnu

This will only install rust and its tools for your target architecture, but some additional tools might be needed for cross-compiling.

Windows

In this section, is the target architecture (either or ). It will explain how to cross compile using rustup.

  1. Install
  2. Run to install rust standard library for your architecture.
  3. Finally, tell cargo where to find the MinGW-w64 gcc/ar by adding the following to your :

Finally, you can cross compile for windows by passing the --target $ARCH-pc-windows-gnu to cargo:

$ # Build
$ cargo build --release --target "$ARCH-pc-windows-gnu"
$ # Run unit tests under wine
$ cargo test --target "$ARCH-pc-windows-gnu"

Currently building executables using MinGW 6 and the toolchains installed by rustup is broken. To fix it, execute

for lib in crt2.o dllcrt2.o libmsvcrt.a; do cp -v /usr/x86_64-w64-mingw32/lib/$lib $HOME/.rustup/toolchains/$CHANNEL-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/; done

where is the update channel (, or )

Unofficial packages

The unofficial repository archlinuxcn has rust-nightly and Rust std library for i686, ARM, ARMv7, Windows 32 and 64 so you can just install the one you want then enjoy cross-compiling. However, you have to find an ARM toolchain by yourself. For Windows 32bit targets, you will need to get a libgcc_s_dw2-1.dll (provided by ) to build and run.

Cargo

Cargo, Rust's package manager, is part of the rust package. The nightly version is available in the AUR as part of . If you use , it already includes cargo.

Cargo is a tool that allows Rust projects to declare their various dependencies, and ensure that you will always get a repeatable build. You are encouraged to read the official guide.

Usage

To create a new project using Cargo:

$ cargo new hello_world 

This creates a directory with a default file, set to build an executable.

Optimizing for native CPU platform

In order to instruct Cargo to always compile optimal code for your CPU platform, you can achieve this by adding a flag to . Please be aware that the resulting binaries can not be distributed for use on other computers, and might even fail on your own system if you decide to change your CPU in the future.

Find out which target platform is used by default on your installation:

$ rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)

In this example, we are using rust on the platform.

Instruct Cargo to always compile code optimized for the native CPU platform:

sccache

Compilation times can be greatly reducing by using sccache ( package). This will maintain a local cache of compiler artifacts, eliminating the need to recompile code that has not changed since the last time it was compiled.

To enable sccache, you can use environment variable:

or

RUSTC_WRAPPER=sccache cargo build

Alternatively, add the following configuration to :

~/.cargo/config
[build]
rustc-wrapper = "sccache"

IDE support

Tools

See https://www.rust-lang.org/tools for the recommended tools of the Rust project.

RLS

RLS used to provide a Language Server Protocol implementation for Rust, providing IDEs, editors, and other tools with information about Rust programs. It supported functionality such as 'goto definition', symbol search, reformatting, and code completion, and enables renaming and refactorings.

RLS is included in the rust package. To install RLS using rustup:

$ rustup component add rls rust-analysis rust-src

rust-analyzer

rust-analyzer is an experimental Language Server Protocol implementation for Rust which is meant to replace RLS.

It is available as the package, and the latest Git version is available as .

rust-analyzer needs the source code of the standard library. If it is not present, rust-analyzer will attempt to install it automatically using rustup. To install the source code manually using rustup, run the following command:

$ rustup component add rust-src

Racer

Racer provides code completion support for editors and IDEs. It has been superseded by RLS (which uses Racer as a fallback).

It requires that you also install a copy of the Rust source code, which you can obtain in one of several ways:

  • With rustup:
  • From the AUR: rust-src-gitAUR or , in this case you must set the environment var.

After installing the source code, you can either use to install racer or obtain it from the repos (rust-racerAUR).

$ cargo +nightly install racer

Clippy

Clippy takes advantage of compiler plugin support to provide a large number of additional lints for detecting and warning about a larger variety of errors and non-idiomatic Rust.

Clippy is included in the rust package. To install it with rustup use:

$ rustup component add clippy

Rustfmt

Rustfmt is a tool to format Rust code according to the official style guidelines.

Rustfmt is included in the rust package. To install it with rustup use:

$ rustup component add rustfmt

Atom

Atom support for Rust programming is provided by the ide-rust plugin (requires rustup).

IntelliJ IDEA

IntelliJ IDEA has a Rust plugin. The same plugin also works with CLion.

If using rustup, use rustup to download the source (), and then select as the toolchain location.

If using Rust from the official Arch Linux software repository, select /usr/bin as the toolchain location and as the stdlib sources location.

Visual Studio Code

Visual Studio Code support for Rust can be obtained via rust-lang.rls extension (requires rustup). If using rust-analyzer, use the matklad.rust-analyzer extension instead.

Vim

Vim support for Rust can be obtained via the official rust.vim plugin, which provides file detection, syntax highlighting, formatting and support for the Syntastic syntax checking plugin. Many completion engines have Rust support, like coc (via the coc.rls plugin) and YouCompleteMe.

Emacs

Emacs support for Rust can be obtained via the official rust-mode plugin.

Kate

Kate support for Rust is achieved using Language Server Protocol. It uses rust-analyzer by default; all you need to do is install it along with the Rust source.

GNOME Builder

GNOME Builder support for Rust is achieved using Language Server Protocol. It uses rust-analyzer by default; all you need to do is install it along with the Rust source.

gollark: *it is not very good*
gollark: Which means that people just convert them to or from letter grades they can deal with mentally so the only difference is some wasted effort.
gollark: In my country schools and some exams have switched to NUMBER grades for no good reason.
gollark: Join the Army! Be yelled at by people you *don't* know and possibly die for some arbitrary country!
gollark: Are these "CB hollies" that rare?

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.