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
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
sudo. For information about the sudo 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.
Download
rustup, which manages the Rust download process.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shNote For those who do not want to usecurl, it is also possible to downloadrustup-initdirectly. A list of all the versions ofrustup-initcan be found on Rust’s installation methods page. For Ubuntu systems, select thex86_64-unknown-linux-gnufile.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 installationIn 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 enter2to 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
envfile or log in to a new console session. Then verify the correct version of Rust has been installed by using theversionflag.source $HOME/.cargo/env rustc --versionYou should see a similar output:
rustc 1.50.0 (cb75ad5db 2021-02-10)If the
versioncommand still does not work, manually add the~/.cargo/bindirectory to thePATHvariable in~/.bashrc. Source the.bashrcfile and try the command again.- File: ~/.bashrc
1 2 3 4... export PATH="$HOME/.cargo/bin:$PATH" ...
Note It is also possible to use Git to install Rust. Clone the Rust GitHub repository and runcargo run --release. See therustupinstallation page for more information.
Updating or Uninstalling Rust
To update Rust, use the
rustuptool.rustup updateTo 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
projectsto 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 helloCreate a new source file named
hello.rsinside thehellodirectory. All Rust files must end with the.rsextension.touch hello.rsOpen the
hello.rsfile in a text editor, and add the code required to display “Hello, World!” to themainfunction. Themainfunction 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 meansprintlnis a macro and not an actual function. - The arguments passed to
printlnare 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 4fn main() { println!("Hello, world!"); }
- Parentheses
Save and close the
hello.rsfile.Compile the program using the
rustccompiler. 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 themainroutine, minus the.rsextension.rustc hello.rsRun the program from the
hellodirectory by specifying the name of the executable../helloProvided 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.rsfile 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
versionflag.cargo --versioncargo 1.50.0 (f04e7fab7 2021-02-04)Create a new project using the
newcommand. The following example indicates how to create a project namedcargo_project.cargo new cargo_projectCompile and build a project, linking in any dependencies, using the
buildcommand.cargo buildCompile, build, and run any Rust project using the
runcommand. If the source code has not changed since the last build, the compile and build steps are skipped.cargo runTo use Cargo to test a Rust project, run the following command.
cargo testUse Cargo to build documentation for a project using the
doccommand.cargo docPublish a package to the Crates repository using the
publishcommand.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