Difference between revisions of "Rust"

From ArchWiki
Jump to navigation Jump to search
(→‎Usage: Replace rustup default stable with more generic rustup default toolchain.)
 
(39 intermediate revisions by 23 users not shown)
Line 2: Line 2:
 
[[ja:Rust]]
 
[[ja:Rust]]
 
[[ru:Rust]]
 
[[ru:Rust]]
 +
{{Related articles start}}
 +
{{Related|Rust package guidelines}}
 +
{{Related articles end}}
 +
 
From [[Wikipedia:Rust (programming language)|Wikipedia]]:
 
From [[Wikipedia:Rust (programming language)|Wikipedia]]:
:''[http://rust-lang.org/ 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.''
+
:[http://rust-lang.org/ 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++.
:''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 ==
 
== Core language ==
 
=== Rust Core Library ===
 
=== Rust Core Library ===
  
The [https://doc.rust-lang.org/core/ 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. Developers looking to target software for embedded platforms may forego the standard library with {{ic|#[nostd]}} to exclusively use the no-batteries-included core library for smaller binary sizes and improved performance. However, using {{ic|#[nostd]}} limits the amount of software support that you can get from the larger Rust community as a majority of libraries require the standard library.  
+
The [https://doc.rust-lang.org/core/ 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. Developers looking to target software for embedded platforms may forego the standard library with {{ic|#![no_std]}} to exclusively use the no-batteries-included core library for smaller binary sizes and improved performance. However, using {{ic|#![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 ===
 
=== Rust Standard Library ===
Line 15: Line 18:
 
The [http://doc.rust-lang.org/std/index.html 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 {{ic|Vec}}, {{ic|Iterator}}, {{ic|Option}}, {{ic|Result}}, and {{ic|String}} types; a vast amount of methods for language primitives; a large number of standard macros; I/O and multithreading support; heap allocations with {{ic|Box}}; and many more high level features not available in the core library.
 
The [http://doc.rust-lang.org/std/index.html 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 {{ic|Vec}}, {{ic|Iterator}}, {{ic|Option}}, {{ic|Result}}, and {{ic|String}} types; a vast amount of methods for language primitives; a large number of standard macros; I/O and multithreading support; heap allocations with {{ic|Box}}; and many more high level features not available in the core library.
  
=== Release Cycle ===
+
=== 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 to stabilize new features for use with stable Rust.
 
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 to stabilize new features for use with stable Rust.
Line 21: Line 24:
 
== Installation ==
 
== Installation ==
  
=== Native Installation ===
+
There are two choices for a rust installation, one is supported by Arch Linux, while the other is officially supported by Rust.
  
To [[install]] the latest stable version of Rust from the official Arch Linux software repository, [[install]] the {{Pkg|rust}} package. This will install the rustc compiler.
+
=== Native installation ===
  
There's also development version of the Rust compiler available from [[AUR]]. Use {{AUR|rust-nightly-bin}} for prebuilt generic binaries or {{AUR|rust-git}} to build the compiler with system libraries. Prebuilt, periodically updated rust-git packages are also available from the [[Unofficial user repositories#rust-git|unofficial repo]].
+
To [[install]] the latest stable version of Rust from the official Arch Linux software repository, [[install]] the {{Pkg|rust}} package. This will install the {{ic|rustc}} compiler and [[#Cargo|Cargo]].
 +
 
 +
There's also development version of the Rust compiler available from [[AUR]]. Use {{AUR|rust-nightly-bin}} for prebuilt generic binaries or {{AUR|rust-git}} to build the compiler with system libraries.
  
 
=== Rustup ===
 
=== Rustup ===
Line 31: Line 36:
 
The official and recommended method of installing Rust for the purpose of developing software is to use the [https://www.rustup.rs/ Rustup toolchain manager], written in Rust.
 
The official and recommended method of installing Rust for the purpose of developing software is to use the [https://www.rustup.rs/ Rustup toolchain manager], written in Rust.
  
The benefits to 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). It is also important to note that tools such as [https://github.com/Manishearth/rust-clippy Clippy] require compiler plugin support, which is only supported in nightly builds of Rust.
+
The benefits to 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).
 
 
One has 2 choices for a rustup installation, one is officially supported by Rust, while the other is supported by Arch Linux.
 
  
 
==== Upstream installation script ====
 
==== Upstream installation script ====
  
Download the file with {{ic|curl -f <nowiki>https://sh.rustup.rs</nowiki> > rust.sh}}, view it: {{ic|less ./rust.sh}}, and run the script {{ic|./rust.sh}} to start rustup installation. The script makes PATH changes only to login shell [[Bash#Invocation|configuration files]].  You need to {{ic|source $HOME/.cargo/env}} until you logout and login back into the system.
+
Download the file with {{ic|curl -f <nowiki>https://sh.rustup.rs</nowiki> > rust.sh}}, view it: {{ic|less ./rust.sh}}, and run the script {{ic|./rust.sh}} to start rustup installation. The script makes PATH changes only to login shell [[Bash#Invocation|configuration files]].  You need to {{ic|source ~/.cargo/env}} until you logout and login back into the system.
 
To update rustup afterwards, run {{ic|rustup self update}}.
 
To update rustup afterwards, run {{ic|rustup self update}}.
  
 
The script installs and activates the default toolchain by default (the one used by the {{pkg|rust}} package, so there is no need to manually install it to start using Rust.
 
The script installs and activates the default toolchain by default (the one used by the {{pkg|rust}} package, so there is no need to manually install it to start using Rust.
  
{{warning|Running {{ic|curl ''some-url'' {{!}} sh}}, as the Rust documentation suggests, is considered as a security risk by some, because it executes unknown and/or untrusted code, that might even be corrupted during the download. Therefore it is recommended to manually download the script and check it, before executing it.}}
+
{{Warning|Running {{ic|curl ''some-url'' {{!}} sh}}, as the Rust documentation suggests, is considered as a security risk, because it executes unknown code, that might even be corrupted during the download. Therefore it is recommended to manually download the script and check it, before executing it.}}
  
{{Note|Please make sure that you have added the $HOME/.cargo/bin to your PATH before you run the rustup command.}}
+
{{Note|Please make sure that {{ic|~/.cargo/bin}} is in your {{ic|PATH}} when you run {{ic|rustup}}.}}
  
 
==== Arch Linux package ====
 
==== Arch Linux package ====
Line 52: Line 55:
 
This package has the advantage that the various Rust executables live in {{ic|/usr/bin}}, instead of {{ic|~/.cargo/bin}}, removing the need to add another directory to your {{ic|PATH}}.
 
This package has the advantage that the various Rust executables live in {{ic|/usr/bin}}, instead of {{ic|~/.cargo/bin}}, removing the need to add another directory to your {{ic|PATH}}.
  
{{note|The {{pkg|rustup}} package does '''not''' install a toolchain by default. The user needs to install a toolchain manually, for the Rust commands to do anything.}}
+
{{note|The {{pkg|rustup}} package does '''not''' install a toolchain by default. It provides instead symlinks between {{ic|/usr/bin/rustup}} to the common binaries such as {{ic|/usr/bin/rustc}} and {{ic|/usr/bin/cargo}}. As stated above, The user still needs to install a toolchain manually, for these Rust commands to do anything.}}
 +
 
 +
In order to install the toolchain, you need to tell rustup which version to use: {{ic|stable}} or {{ic|nightly}}.
 +
 
 +
Example:
 +
{{bc|$ rustup default stable}}
  
 
==== Usage ====
 
==== Usage ====
  
You might need to manually install a toolchain, e.g. {{ic|stable}}, {{ic|beta}} or {{ic|nightly}}. You also need to do this if you want to use/test another toolchain.
+
You might need to manually install a toolchain, e.g. {{ic|stable}}, {{ic|beta}}, {{ic|nightly}} or {{ic|1.23.0}}. You also need to do this if you want to use/test another toolchain.
  $ rustup install ''toolchain''
+
  $ rustup toolchain install ''toolchain''
  
 
You can now run the Rust commands by running, {{ic|rustup run ''toolchain'' ''command''}}. However, to use these commands directly, you need to activate the toolchain:  
 
You can now run the Rust commands by running, {{ic|rustup run ''toolchain'' ''command''}}. However, to use these commands directly, you need to activate the toolchain:  
Line 65: Line 73:
  
 
{{hc|$ rustc -V |
 
{{hc|$ rustc -V |
rustc 1.9.0 (e4e8b6668 2016-05-18)
+
rustc 1.26.0 (a77568041 2018-05-07)
 +
}}
 +
 
 +
{{note|Rustup does not install some Rust commands that the {{pkg|rust}} package does include, such as {{ic|rustfmt}} and {{ic|rls}}. They are not included because this allows the Rust maintainers to ship a nightly of Rust with a broken {{ic|rustfmt}}/{{ic|rls}}. To install them, run {{ic|rustup component add rls-preview}} and {{ic|rustup component add rustfmt-preview}} respectively. This will also suspend updates of the nightly channel, if they break {{ic|rustfmt}}/{{ic|rls}}.
 +
}}
 +
 
 +
{{note|Rust does not do its own linking, and so you’ll need to have a linker installed. You can use {{Pkg|gcc}}, otherwise Rust will generate the following {{ic| error: linker `cc` not found.}}
 
}}
 
}}
  
Line 82: Line 96:
 
}}
 
}}
  
== Cross Compiling ==
+
== Cross compiling ==
  
 
=== Using rustup ===
 
=== Using rustup ===
Line 90: Line 104:
 
For instance, if you want to install rust using the stable channel for windows, using the gnu compiler, you will need to do :  
 
For instance, if you want to install rust using the stable channel for windows, using the gnu compiler, you will need to do :  
  
{{bc|<nowiki>
+
$ rustup install stable-x86_64-pc-windows-gnu
$ rustup install stable-x86_64-pc-windows-gnu
 
</nowiki>}}
 
  
 
This will only installs rust and its tools for your target architecture, but some additional tools might be needed for cross compiling.
 
This will only installs rust and its tools for your target architecture, but some additional tools might be needed for cross compiling.
Line 111: Line 123:
  
 
Finally, you can cross compile for windows by passing the {{ic|--target $ARCH-pc-windows-gnu}} to cargo:
 
Finally, you can cross compile for windows by passing the {{ic|--target $ARCH-pc-windows-gnu}} to cargo:
{{bc|<nowiki>
+
 
$ # Build
+
$ # Build
$ cargo build --release --target "$ARCH-pc-windows-gnu"
+
$ cargo build --release --target "$ARCH-pc-windows-gnu"
$ # Run unit tests under wine
+
$ # Run unit tests under wine
$ cargo test --target "$ARCH-pc-windows-gnu"
+
$ cargo test --target "$ARCH-pc-windows-gnu"
</nowiki>}}
+
 
 +
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 {{ic|CHANNEL}} is the update channel ({{ic|stable}}, {{ic|beta}} or {{ic|nightly}})
  
 
=== Unofficial packages ===
 
=== Unofficial packages ===
  
The [[Unofficial user repositories#archlinuxcn|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.
+
The [[Unofficial user repositories#archlinuxcn|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'll need to get a libgcc_s_dw2-1.dll to build and run.
  
 
== Cargo ==
 
== Cargo ==
Line 131: Line 148:
  
 
To create a new project using Cargo:
 
To create a new project using Cargo:
{{bc|$ cargo new hello_world --bin}}
 
  
This creates a directory with a default {{ic|Cargo.toml}} file, set to build an executable (because we included {{ic|--bin}}, otherwise it would build a library).
+
$ cargo new hello_world
 +
 
 +
This creates a directory with a default {{ic|Cargo.toml}} file, set to build an executable.
  
 
{{Note|Cargo uses this {{ic|Cargo.toml}} as a manifest containing all of the metadata required to compile your project.
 
{{Note|Cargo uses this {{ic|Cargo.toml}} as a manifest containing all of the metadata required to compile your project.
Line 141: Line 159:
 
version = "0.1.0"
 
version = "0.1.0"
 
authors = ["Your Name <you@example.com>"]
 
authors = ["Your Name <you@example.com>"]
 +
edition = "2018"
 +
 +
[dependencies]
 
}}
 
}}
 
}}
 
}}
  
== IDE Support ==
+
== IDE support ==
  
 
=== Tools ===
 
=== Tools ===
Line 155: Line 176:
 
After installing the source code, you can either use {{ic|Cargo}} to install racer or obtain it from the repos ({{pkg|rust-racer}}).
 
After installing the source code, you can either use {{ic|Cargo}} to install racer or obtain it from the repos ({{pkg|rust-racer}}).
  
  $ cargo install racer
+
  $ cargo +nightly install racer
  
 
==== Clippy ====
 
==== Clippy ====
  
[https://github.com/Manishearth/rust-clippy Clippy] takes advantage of compiler plugin support in Nightly builds of Rust to provide a large number of additional lints for detecting and warning about a larger variety of errors and non-idiomatic Rust. Because it requires support for compiler plugins in order to operate, clippy will not work when compiling with the stable Rust compiler.
+
[https://github.com/Manishearth/rust-clippy Clippy] takes advantage of compiler plugin support in Nightly builds of Rust to provide a large number of additional lints for detecting and warning about a larger variety of errors and non-idiomatic Rust. Install clippy using rustup with:
  
  $ cargo install clippy
+
  $ rustup component add clippy-preview
  
 
=== Editors ===
 
=== Editors ===
Line 167: Line 188:
 
==== Atom ====
 
==== Atom ====
  
Atom supports Rust programming when both of the next plugins installed: [https://atom.io/packages/language-rust language-rust] and [https://atom.io/packages/linter-rust linter-rust].
+
[[Atom]] supports Rust programming when both of the next plugins installed: [https://atom.io/packages/language-rust language-rust] and [https://atom.io/packages/linter-rust linter-rust].
  
 
==== IntelliJ IDEA ====
 
==== IntelliJ IDEA ====
Line 175: Line 196:
 
==== Visual Studio Code ====
 
==== Visual Studio Code ====
  
Support for Rust can be obtained by installing the [https://marketplace.visualstudio.com/items?itemName=kalitaalexey.vscode-rust Rust] extension.
+
[[Visual Studio Code]] support for Rust can be obtained via [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust rust-lang.rls] extension(requires rustup) or the [https://marketplace.visualstudio.com/items?itemName=kalitaalexey.vscode-rust kalitaalexey.vscode-rust] extension.
  
 
==== Vim ====
 
==== Vim ====
  
Vim support for Rust is enabled via the official [https://github.com/rust-lang/rust.vim rust.vim] plugin.
+
[[Vim]] support for Rust can be obtained via the official [https://github.com/rust-lang/rust.vim rust.vim] plugin.
  
====Emacs====
+
==== Emacs ====
  
Emacs support for Rust is enabled via the offical [https://github.com/rust-lang/rust-mode rust-mode] plugin or the {{AUR|emacs-rust-mode}} package.
+
[[Emacs]] support for Rust can be obtained via the official [https://github.com/rust-lang/rust-mode rust-mode] plugin or the {{AUR|emacs-rust-mode}} package.
  
 +
==== Kate ====
  
====Kate====
+
Kate support for Rust is installed by default by the {{Pkg|kate}} package, but also requires extra configurations.
 
+
First, install the package {{pkg|rust-racer}}
Kate support for Rust is enabled via the official [https://github.com/rust-lang/kate-config kate] plugin. It is installed by defaut by the {{Pkg|kate}} package, require the installation of {{AUR|racer}} and manual activation.
+
Second, get the Rust source:
 +
* With rustup: {{ic|rustup component add rust-src}}
 +
* or From the AUR: {{Aur|rust-src}} or {{Aur|rust-nightly-src}}, in this case you must set the {{ic|RUST_SRC_PATH}} environment var.
 +
Third, configure Kate by clicking Settings -> Configure Kate,  then navigate to Application -> Plugins,  then tick the 'Rust code completion',  then you will see Application -> Rust code completion in the left sidebar, navigation to it, and assign 'Rust source tree location' to {{ic|$HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src}}
 +
if you install the Rust source with rustup.
  
 
== See also ==
 
== See also ==
Line 196: Line 222:
 
* [http://doc.rust-lang.org/stable/book/ Official Rust Book]
 
* [http://doc.rust-lang.org/stable/book/ Official Rust Book]
 
* [https://doc.rust-lang.org/std/ Standard Library API Lookup]
 
* [https://doc.rust-lang.org/std/ Standard Library API Lookup]
* [http://rustbyexample.com/ Examples with small descriptions]
+
* [https://doc.rust-lang.org/stable/rust-by-example/ Examples with small descriptions]
 
* [https://github.com/ctjhoa/rust-learning Page listing of Rust tutorials]
 
* [https://github.com/ctjhoa/rust-learning Page listing of Rust tutorials]
 
* [https://crates.io/ Libraries(crates) available through Cargo]
 
* [https://crates.io/ Libraries(crates) available through Cargo]

Latest revision as of 03:26, 16 August 2019

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. 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, Iterator, Option, Result, 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 to stabilize new features for use with stable Rust.

Installation

There are two choices for a rust installation, one is supported by Arch Linux, while the other is officially supported by 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 rustc compiler and Cargo.

There's also development version of the Rust compiler available from AUR. Use rust-nightly-binAUR for prebuilt generic binaries or rust-gitAUR 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 benefits to 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).

Upstream installation script

Download the file with curl -f https://sh.rustup.rs > rust.sh, view it: less ./rust.sh, 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.

Warning: Running curl some-url | sh, as the Rust documentation suggests, is considered as a security risk, because it executes unknown code, that might even be corrupted during the download. Therefore it is recommended to manually download the script and check it, before executing it.
Note: Please make sure that ~/.cargo/bin is in your PATH when you run rustup.

Arch Linux package

rustup is also 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 ~/.cargo/bin, removing the need to add another directory to your PATH.

Note: The rustup package does not install a toolchain by default. It provides instead symlinks between /usr/bin/rustup to the common binaries such as /usr/bin/rustc and /usr/bin/cargo. As stated above, The user still needs to install a toolchain manually, for these Rust commands to do anything.

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

Example:

$ rustup default stable

Usage

You might need to manually install a toolchain, e.g. stable, beta, nightly or 1.23.0. 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, rustup run toolchain command. However, to use these commands directly, you need to activate the toolchain:

$ rustup default toolchain

Check the installed Rust version using rustc -V:

$ rustc -V 
rustc 1.26.0 (a77568041 2018-05-07)
Note: Rustup does not install some Rust commands that the rust package does include, such as rustfmt and rls. They are not included because this allows the Rust maintainers to ship a nightly of Rust with a broken rustfmt/rls. To install them, run rustup component add rls-preview and rustup component add rustfmt-preview respectively. This will also suspend updates of the nightly channel, if they break rustfmt/rls.
Note: Rust does not do its own linking, and so you’ll need to have a linker installed. You can use gcc, otherwise Rust will generate the following error: linker `cc` not found.

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

Using rustup

You can easily cross-compile using rustup. rustup supports many crosscompile 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 install stable-x86_64-pc-windows-gnu

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

Windows

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

  1. Install mingw-w64-gccAUR and wine
  2. Add a binfmt definition for windows executables either manually or by installing binfmt-wineAUR[broken link: package not found].
  3. If you are using rustup, you can run rustup install stable-$ARCH-pc-windows-gnu and rustup target add $ARCH-pc-windows-gnu to install rust and rust standard library for your architecture. If you are not using rustup, 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/lib/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"

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 CHANNEL is the update channel (stable, beta or nightly)

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'll need to get a libgcc_s_dw2-1.dll 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 cargo-nightly-binAUR. If you use rustup, 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 

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

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>"]
edition = "2018"

[dependencies]

IDE support

Tools

Racer

The Racer autocomplete engine is the current best method of gaining autocomplete support. However, it requires that you also install a copy of the Rust source code, which you can obtain in one of several ways:

  • With rustup: rustup component add rust-src
  • From the AUR: rust-srcAUR or rust-nightly-srcAUR, in this case you must set the RUST_SRC_PATH environment var.

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

$ cargo +nightly install racer

Clippy

Clippy takes advantage of compiler plugin support in Nightly builds of Rust to provide a large number of additional lints for detecting and warning about a larger variety of errors and non-idiomatic Rust. Install clippy using rustup with:

$ rustup component add clippy-preview

Editors

Atom

Atom supports Rust programming when both of the next plugins installed: language-rust and linter-rust.

IntelliJ IDEA

IntelliJ IDEA has a Rust plugin. The same plugin also works with CLion. When configuring the toolchain, use rustup to download the source (rustup component add rust-src), and then select ~/.rustup/toolchains/<your toolchain>/bin as the toolchain location.

Visual Studio Code

Visual Studio Code support for Rust can be obtained via rust-lang.rls extension(requires rustup) or the kalitaalexey.vscode-rust extension.

Vim

Vim support for Rust can be obtained via the official rust.vim plugin.

Emacs

Emacs support for Rust can be obtained via the official rust-mode plugin or the emacs-rust-modeAUR package.

Kate

Kate support for Rust is installed by default by the kate package, but also requires extra configurations. First, install the package rust-racer Second, get the Rust source:

  • With rustup: rustup component add rust-src
  • or From the AUR: rust-srcAUR or rust-nightly-srcAUR, in this case you must set the RUST_SRC_PATH environment var.

Third, configure Kate by clicking Settings -> Configure Kate, then navigate to Application -> Plugins, then tick the 'Rust code completion', then you will see Application -> Rust code completion in the left sidebar, navigation to it, and assign 'Rust source tree location' to $HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src if you install the Rust source with rustup.

See also