Installing and Using Rust
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
This guide explains how to install Rust, a popular programming language designed to maximize performance and safety. It also discusses how to create, compile, and run a simple Rust project. Rust is somewhat similar to C++, although it is able to guarantee memory and thread safety. Rust was originally developed for use at Mozilla Research, but it has recently gained in popularity throughout the software industry. For many years now, Rust has been rated one of the top programming languages in industry surveys.
Before You Begin
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
This guide uses
sudo
wherever possible. Complete the sections of Linode’s Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services. Do not follow the Configure a Firewall section yet–this guide includes firewall rules specifically for an OpenVPN server.Update the system:
sudo apt-get update && sudo apt-get upgrade
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. For information about thesudo
command, see the Users and Groups guide.
Advantages of the Rust Programming Language
Rust runs as quickly as C++ does, but is safer to use. This is because it emphasizes memory and thread safety through the use of a borrow checker and reference validation. Unlike many memory-safe languages, Rust does not use a garbage collector, which means it is relatively memory efficient. Rust allows direct access to the hardware layer and control over the memory layout. This makes it a good choice for embedded systems. It is suitable for highly concurrent systems and for applications requiring large-system integrity. Rust includes a library that allows for calls to, or from, C++ with very little overhead.
Installing Rust
The typical, and most straightforward, way to install Rust is by using rustup
. This is Rust’s main installation program and version manager. These instructions are designed for Ubuntu, but are generally applicable to most Linux distributions.
NoteTo experiment with Rust before downloading it, try the Rust Playground. This is an open source educational program that allows beginners to write and run simple Rust programs.
Download
rustup
, which manages the Rust download process.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Note
For those who do not want to usecurl
, it is also possible to downloadrustup-init
directly. A list of all the versions ofrustup-init
can be found on Rust’s installation methods page. For Ubuntu systems, select thex86_64-unknown-linux-gnu
file.Rust displays some background details about the installation, including the location of default directories and environmental settings for its various components. It then provides the following three choices:
1) Proceed with installation (default) 2) Customize installation 3) Cancel installation
In most cases, customization is not required. To accept the defaults and proceed with the installation, enter
1
. To customize the Rust installation, first carefully review the information about the various settings and note any necessary changes. Then enter2
to begin the customization process.When the process is complete, Rust confirms the installation is successful.
Rust is installed now. Great!
To apply the environment changes, either source the Rust
env
file or log in to a new console session. Then verify the correct version of Rust has been installed by using theversion
flag.source $HOME/.cargo/env rustc --version
You should see a similar output:
rustc 1.50.0 (cb75ad5db 2021-02-10)
If the
version
command still does not work, manually add the~/.cargo/bin
directory to thePATH
variable in~/.bashrc
. Source the.bashrc
file and try the command again.- File: ~/.bashrc
1 2 3 4
... export PATH="$HOME/.cargo/bin:$PATH" ...
NoteIt is also possible to use Git to install Rust. Clone the Rust GitHub repository and runcargo run --release
. See therustup
installation page for more information.
Updating or Uninstalling Rust
To update Rust, use the
rustup
tool.rustup update
To remove Rust from your system, run the following command.
rustup self uninstall
Using Rust
Rust support is available for many editors, including Vi/Vim, VS Code, and Emacs.
Building a Small Project in Rust
As with most programming languages, it is easiest to learn the basics with a simple “Hello, World” program. This short tutorial explains how to create, write, compile, and run a program in Rust. Compiling and running a program are separate steps. A program must be compiled before it can run.
Create a directory named
projects
to store all of your Rust projects. Then, create a sub-directory for the “Hello World” project and move into the new directory.mkdir ~/projects cd ~/projects mkdir hello cd hello
Create a new source file named
hello.rs
inside thehello
directory. All Rust files must end with the.rs
extension.touch hello.rs
Open the
hello.rs
file in a text editor, and add the code required to display “Hello, World!” to themain
function. Themain
function is the first code to execute in any Rust project. Here are a few things to note about this program:- Parentheses
()
are used to enclose any function parameters. There are no parameters in this case. - The body of the function is enclosed inside curly braces
{}
. - Inside the function,
println!
is a macro that sends text to the standard output device. The!
symbol at the end of the macro name meansprintln
is a macro and not an actual function. - The arguments passed to
println
are contained inside the trailing parentheses. Here there is only one parameter, which contains the text to be printed. - Each expression ends with a
;
symbol.
Consult Rust’s documentation for further discussion of Rust’s style conventions.
- File: ~/projects/hello/hello.rs
1 2 3 4
fn main() { println!("Hello, world!"); }
- Parentheses
Save and close the
hello.rs
file.Compile the program using the
rustc
compiler. Provide the name of the file as an argument torustc
. This creates an executable namedhello
. The name of the executable is the name of the source file containing themain
routine, minus the.rs
extension.rustc hello.rs
Run the program from the
hello
directory by specifying the name of the executable../hello
Provided there are no errors, the program displays “Hello, world!” in the console window. If the program does not work as expected, verify the syntax of the
hello.rs
file is correct.Hello, world!
Using the Cargo Build Tool and Package Manager
The lightweight process in the previous section works well with small applications because it imposes no extra overhead. For larger applications with dependencies, it is preferable to use
Cargo, Rust’s build tool and package manager. Cargo, which is installed by rustup
alongside Rust, can be used to simplify many common development tasks. The following examples demonstrate some of Cargo’s functionality.
Confirm Cargo is installed and verify the version number using the
version
flag.cargo --version
cargo 1.50.0 (f04e7fab7 2021-02-04)
Create a new project using the
new
command. The following example indicates how to create a project namedcargo_project
.cargo new cargo_project
Compile and build a project, linking in any dependencies, using the
build
command.cargo build
Compile, build, and run any Rust project using the
run
command. If the source code has not changed since the last build, the compile and build steps are skipped.cargo run
To use Cargo to test a Rust project, run the following command.
cargo test
Use Cargo to build documentation for a project using the
doc
command.cargo doc
Publish a package to the Crates repository using the
publish
command.cargo publish
For Further Reference
The Rust website provides extensive
documentation, including tutorials, examples, and a link to the Rust Programming Language text. In particular, the
Rustlings Course serves as a good quick introduction to the language. The short tutorial at the bottom of the
Getting Started page is also useful. The Rust documentation also contains guides to the Cargo package manager and the rustc
compiler.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on