Rust - ArchWiki

# Rust

Rust is a systems programming language that runs blazingly fast, prevents nearly all segfaults, and guarantees thread and memory safety.

## Installation

To install the latest stable version of Rust, install the rust package.

If you would like to build the stable or Beta from source, visit Rust Downloads. You may also obtain rust-nightly-binAUR via the AUR for the latest development snapshot (provides Cargo).

An alternative is to use multirustAUR, a tool that allows to install and manage multiple rust installations (stable, beta, nightly), and easily switch between them.

### 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 `rustc`, then run it:

    $ rustc hello.rs && ./hello

    Hello, World!

## Cross Compiling

### Windows

In this section, `$ARCH` is the target architecture (either `x86_64` or `i686`).

1. Install mingw-w64-gcc and wine
2. Add a binfmt definition for windows executables either manually or by installing binfmt-wineAUR.
3. Install a copy of rust's standard library for windows in your rustlib directory (`/usr/local/lib/rustlib` if you're using rust-nightly-binAUR and `/usr/lib/rustlib` if you're using the official rust package). The easiest way to do this is to download the rust installer for windows for your target architecture, install it under wine (`wine start my-rust-installer.msi`) and copy `$INSTALL_DIR/bin/rustlib/$ARCH-pc-windows-gnu` into your rustlib directory.
4. Finally, tell cargo where to find the MinGW-w64 gcc/ar by adding the following to your `~/.cargo/config`:

    ~/.cargo/config

    [target.$ARCH-pc-windows-gnu]
    linker = "/usr/bin/$ARCH-w64-mingw32-gcc"
    ar = "/usr/$ARCH-w64-mingw32/bin/ar"

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"

### unofficial packages

The unofficial repo 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'll need to get a libgcc_s_dw2-1.dll to build and run.

## Cargo

Cargo, Rust's package manager, can be installed as cargo. The nightly version is available in the AUR as cargo-binAUR. If you use multirustAUR, it already includes cargo.

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

### Usage

To create a new project using Cargo:

    $ cargo new hello_world --bin

This creates a directory with a default `Cargo.toml` file, set to build an executable (because we included `--bin`, otherwise it would build a library).

Note: Cargo uses this `Cargo.toml` as a manifest containing all of the metadata required to compile your project.

    Cargo.toml

    [package]
    name = "hello_world"
    version = "0.1.0"
    authors = ["Your Name <you@example.com>"]

## See also

- Official website of the Rust Programming Language
- Wikipedia article
- Examples with small descriptions
- Official starting guide
- Page listing of Rust tutorials
- Libraries(crates) avalaible through Cargo

Category:

- Programming languages