Este contenido no está disponible en el idioma seleccionado.
Using Rust 1.84.1 Toolset
Installing and using Rust 1.84.1 Toolset
Abstract
Providing feedback on Red Hat documentation Copiar enlaceEnlace copiado en el portapapeles!
We appreciate your feedback on our documentation. Let us know how we can improve it.
Submitting feedback through Jira (account required)
- Log in to the Jira website.
- Click Create in the top navigation bar
- Enter a descriptive title in the Summary field.
- Enter your suggestion for improvement in the Description field. Include links to the relevant parts of the documentation.
- Click Create at the bottom of the dialogue.
Chapter 1. Rust Toolset Copiar enlaceEnlace copiado en el portapapeles!
Rust Toolset is a Red Hat offering for developers on Red Hat Enterprise Linux (RHEL). It provides the rustc compiler for the Rust programming language, the Rust package manager Cargo, the rustfmt formatting tool, and required libraries.
Rust Toolset is available as a module for RHEL 8 and as packages for RHEL 9 and 10.
1.1. Rust Toolset components Copiar enlaceEnlace copiado en el portapapeles!
The following components are available as part of Rust Toolset:
| Name | Version | Description |
|---|---|---|
|
| 1.84.1 | The Rust compiler front-end for LLVM. |
|
| 1.84.1 | A build system and dependency manager for Rust. |
|
| 1.84.1 | A tool for automatic formatting of Rust code. |
1.2. Rust Toolset compatibility Copiar enlaceEnlace copiado en el portapapeles!
Rust Toolset is available for {RHEL] on the following architectures:
- AMD and Intel 64-bit
- 64-bit ARM
- IBM Power Systems, Little Endian
- 64-bit IBM Z
1.3. Installing Rust Toolset Copiar enlaceEnlace copiado en el portapapeles!
Complete the following steps to install Rust Toolset including all development and debugging tools and dependent packages. Note that Rust Toolset has a dependency on LLVM Toolset.
Prerequisites
- All available Red Hat Enterprise Linux updates are installed.
Procedure
Install Rust Toolset:
On RHEL 8, enter:
yum module install rust-toolset
# yum module install rust-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow On RHEL 9 and 10, enter:
dnf install rust-toolset
# dnf install rust-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.4. Installing Rust documentation Copiar enlaceEnlace copiado en el portapapeles!
The Rust Programming Language book is available as installable documentation.
Procedure
Install the
rust-docpackage:On RHEL 8, enter:
yum install rust-doc
# yum install rust-docCopy to Clipboard Copied! Toggle word wrap Toggle overflow On RHEL 9 and 10, enter:
dnf install rust-doc
# dnf install rust-docCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Use a browser that is installed on the same host to display the documentation:
-
The Rust Programming Language book:
/usr/share/doc/rust/html/index.html -
The API documentation for all Rust code packages:
/usr/share/doc/rust/html/std/index.html
-
The Rust Programming Language book:
1.5. Installing Cargo documentation Copiar enlaceEnlace copiado en el portapapeles!
The Cargo, Rust’s Package Manager book is available as installable documentation for Cargo.
Procedure
Install the
cargo-docpackage:On RHEL 8, enter:
yum install cargo-doc
# yum install cargo-docCopy to Clipboard Copied! Toggle word wrap Toggle overflow On RHEL 9 and 10, enter:
dnf install cargo-doc
# dnf install cargo-docCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
Open
/usr/share/doc/cargo/html/index.htmlin a browser that is installed on the same host.
Chapter 2. The Cargo build tool Copiar enlaceEnlace copiado en el portapapeles!
Cargo is a build tool and front end for the Rust compiler rustc and a package and dependency manager. It allows Rust projects to declare dependencies with specific version requirements, resolves the full dependency graph, downloads packages, and builds, and tests your entire project.
2.1. The Cargo directory structure and file placements Copiar enlaceEnlace copiado en el portapapeles!
The Cargo build tool uses set conventions for defining the directory structure and file placement within a Cargo package. Running the cargo new command generates the package directory structure and templates for both a manifest and a project file. By default, it also initializes a new Git repository in the package root directory.
For a binary program, Cargo creates a directory <project_name> containing a text file named Cargo.toml and a subdirectory src containing a text file named main.rs.
2.2. Creating a Rust project Copiar enlaceEnlace copiado en el portapapeles!
Create a new Rust project that is set up according to the Cargo conventions. For more information on Cargo conventions, see The Cargo directory structure and file placements.
Procedure
Create a Rust project:
cargo new --bin <project_name>
$ cargo new --bin <project_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<project_name>with your project name.-
To edit the project code, edit the main executable file
main.rsand add new source files to thesrcsubdirectory.
2.3. Creating a Rust library project Copiar enlaceEnlace copiado en el portapapeles!
Complete the following steps to create a Rust library project by using the Cargo build tool.
Procedure
Create a Rust library project:
cargo new --lib <project_name>
$ cargo new --lib <project_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<project_name>with the name of your Rust project.-
To edit the project code, edit the
src/lib.rssource file.
2.4. Building a Rust project Copiar enlaceEnlace copiado en el portapapeles!
Build your Rust project using the Cargo build tool. Cargo resolves all dependencies of your project, downloads missing dependencies, and compiles it using the rustc compiler.
By default, projects are built and compiled in debug mode. For information on compiling your project in release mode, see Building a Rust project in release mode.
Prerequisites
Procedure
In the Rust project directory, build the project:
cargo build
$ cargo buildCopy to Clipboard Copied! Toggle word wrap Toggle overflow To verify that your Rust program can be built when you do not need to build an executable file, enter:
cargo check
$ cargo checkCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Building a Rust project in release mode Copiar enlaceEnlace copiado en el portapapeles!
Build your Rust project in release mode by using the Cargo build tool. Release mode is optimizing your source code and can therefore increase compilation time while ensuring that the compiled binary will run faster. Use this mode to produce optimized artifacts suitable for release and production.
Cargo resolves all dependencies of your project, downloads missing dependencies, and compiles it by using the rustc compiler.
For information on compiling your project in debug mode, see Building a Rust project.
Prerequisites
Procedure
In the Rust project directory, build the project in release mode:
cargo build --release
$ cargo build --releaseCopy to Clipboard Copied! Toggle word wrap Toggle overflow To verify that your Rust program can be built when you do not need to build an executable file, enter:
cargo check
$ cargo checkCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6. Running a Rust program Copiar enlaceEnlace copiado en el portapapeles!
Run your Rust project by using the Cargo build tool. Cargo first rebuilds your project and then runs the resulting executable file. If used during development, the cargo run command correctly resolves the output path independently of the build mode.
Prerequisites
Procedure
To run a Rust program managed as a project by Cargo, enter in the project directory:
cargo run
$ cargo runCopy to Clipboard Copied! Toggle word wrap Toggle overflow If your program has not been built yet, Cargo builds your program before running it.
2.7. Testing a Rust project Copiar enlaceEnlace copiado en el portapapeles!
Test your Rust program using the Cargo build tool. Cargo first rebuilds your project and then runs the tests found in the project. Test functions must be free, monomorphic, and take no arguments. The function return type must be either () or Result<(), E> where E: Error.
By default, Rust projects are tested in debug mode. For information on testing your project in release mode, see Testing a Rust project in release mode.
Prerequisites
Procedure
-
Add the
#[test]attribute in front of your function. Enter in the project directory:
cargo test
$ cargo testCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.8. Testing a Rust project in release mode Copiar enlaceEnlace copiado en el portapapeles!
Test your Rust program in release mode by using the Cargo build tool. Release mode is optimizing your source code and can therefore increase compilation time while ensuring that the compiled binary will run faster. Use this mode to produce optimized artifacts suitable for release and production.
Cargo first rebuilds your project and then runs the tests found in the project. Test functions must be free, monomorphic, and take no arguments. The function return type must be either () or Result<(), E> where E: Error.
For information on testing your project in debug mode, see Testing a Rust project.
Prerequisites
Procedure
-
Add the
#[test]attribute in front of your function. Enter in the project directory:
cargo test --release
$ cargo test --releaseCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.9. Configuring Rust project dependencies Copiar enlaceEnlace copiado en el portapapeles!
Configure the dependencies of your Rust project by using the Cargo build tool. To specify dependencies for a project managed by Cargo, edit the file Cargo.toml in the project directory and rebuild your project. Cargo downloads the Rust code packages and their dependencies, stores them locally, builds all of the project source code including the dependency code packages, and runs the resulting executable.
Prerequisites
Procedure
In your project directory, edit the
Cargo.tomlfile, and list each dependency in the following format in the[dependencies]section:<crate_name> = <version>
<crate_name> = <version>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rust code packages are called crates.
Rebuild your project:
cargo build
$ cargo buildCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run your project:
cargo run
$ cargo runCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.10. Building documentation for a Rust project Copiar enlaceEnlace copiado en el portapapeles!
Use the Cargo tool to generate documentation from comments in your source code that are marked for extraction. Note that documentation comments are extracted only for public functions, variables, and members.
Prerequisites
Procedure
-
In your code, use three slashes
///at the beginning of a line to mark the line for extracting the comment for documentation. Build the documentation:
cargo doc --no-deps
$ cargo doc --no-depsCopy to Clipboard Copied! Toggle word wrap Toggle overflow The command stores the generated documentation in the
.target/doc/directory.
2.11. Compiling code into a WebAssembly binary with Rust Copiar enlaceEnlace copiado en el portapapeles!
Complete the following steps to install the WebAssembly standard library.
Prerequisites
Procedure
Install the WebAssembly standard library:
On RHEL 8, enter:
yum install rust-std-static-wasm32-unknown-unknown
# yum install rust-std-static-wasm32-unknown-unknownCopy to Clipboard Copied! Toggle word wrap Toggle overflow On RHEL 9 and 10, enter:
dnf install rust-std-static-wasm32-unknown-unknown
# dnf install rust-std-static-wasm32-unknown-unknownCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Use WebAssembly with Cargo:
cargo <command> --target wasm32-unknown-unknown
$ cargo <command> --target wasm32-unknown-unknownCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<command>with the Cargo command you want to run.
2.12. Vendoring Rust project dependencies Copiar enlaceEnlace copiado en el portapapeles!
Create a local copy of the dependencies of your Rust project for offline redistribution and reuse the Cargo build tool. This procedure is called vendoring project dependencies. The vendored dependencies including Rust code packages for building your project on a Windows operating system are located in the vendor directory. Vendored dependencies can be used by Cargo without any connection to the internet.
Prerequisites
Procedure
To vendor your Rust project with dependencies by using Cargo, enter in the project directory:
cargo vendor
$ cargo vendorCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 3. The rustfmt formatting tool Copiar enlaceEnlace copiado en el portapapeles!
With the rustfmt formatting tool, you can automatically format the source code of your Rust programs. You can use rusftmt either as a standalone tool or with Cargo.
For further details, see the rustfmt help pages displayed by the rustfmt --help command.
3.1. Installing rustfmt Copiar enlaceEnlace copiado en el portapapeles!
Complete the following steps to install the rustfmt formatting tool.
Prerequisites
Procedure
Install the
rustfmtpackage:On RHEL 8, enter:
yum install rustfmt
# yum install rustfmtCopy to Clipboard Copied! Toggle word wrap Toggle overflow On RHEL 9 and 10, enter:
dnf install rustfmt
# dnf install rustfmtCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.2. Using rustfmt as a standalone tool Copiar enlaceEnlace copiado en el portapapeles!
Use rustfmt as a standalone tool to format a Rust source file and all its dependencies. As an alternative, use rustfmt with the Cargo build tool. For more information, see Using rustfmt with the Cargo build tool.
Prerequisites
Procedure
Format the Rust source code:
rustfmt <source-file>
$ rustfmt <source-file>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<source_file>with the name of your source file. Alternatively, you can replace<source_file>with standard input. Therustfmtutility then provides its output in standard output.ImportantBy default,
rustfmtmodifies the affected files without displaying details or creating backups. To display details and create backups, runrustfmtwith the--write-modeoption.
3.3. Using rustfmt with the Cargo build tool Copiar enlaceEnlace copiado en el portapapeles!
Use the rustfmt tool with Cargo to format a Rust source file and all its dependencies. As an alternative, use rustfmt as a standalone tool. For more information, see Using rustfmt as a standalone tool.
Prerequisites
Procedure
-
Optional: To change the
rustfmtformatting options, createrustfmt.tomlconfiguration file in the project directory and add your configurations to the file. Format the Rust source code:
cargo fmt
$ cargo fmtCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 4. Container images with Rust Toolset Copiar enlaceEnlace copiado en el portapapeles!
You can build your own Rust Toolset containers from Red Hat Universal Base Images (UBI).
4.1. Creating a custom UBI-based container with Rust Toolset Copiar enlaceEnlace copiado en el portapapeles!
Rust Toolset packages are part of the Red Hat Universal Base Images (UBIs) repositories. To keep the container size small, install only individual packages instead of the entire Rust Toolset.
Prerequisites
- An existing container file. For information on creating Containerfiles, see the Dockerfile reference page.
Procedure
To create a container image containing Rust Toolset, add the following to your container file:
For an image based on RHEL 8, enter:
FROM registry.access.redhat.com/ubi8/ubi:latest RUN yum module install -y rust-toolset
FROM registry.access.redhat.com/ubi8/ubi:latest RUN yum module install -y rust-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow For an image based on RHEL 9, enter:
FROM registry.access.redhat.com/ubi9/ubi:latest RUN yum install -y rust-toolset
FROM registry.access.redhat.com/ubi9/ubi:latest RUN yum install -y rust-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow For an image based on RHEL 10, enter:
FROM registry.access.redhat.com/ubi10/ubi:latest RUN yum install -y rust-toolset
FROM registry.access.redhat.com/ubi10/ubi:latest RUN yum install -y rust-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 5. Changes in Rust 1.84.1 Toolset Copiar enlaceEnlace copiado en el portapapeles!
RHEL provides Rust Toolset in version 1.84.1. Notable enhancements since the previously available version 1.79.0 include:
-
The new
LazyCellandLazyLocktypes delay the initialization until the first use. These extend the earlierOnceCellandOnceLocktypes with the initialization function included in each instance. - The new sort implementations in the standard library improve the runtime performance and compile times. They also try to detect cases where a comparator is not producing a total order, making that panic instead of returning unsorted data.
-
Precise capturing for opaque return types have been added. The new
use<..>syntax specifies the generic parameters and lifetimes used in animpl Traitreturn type. Many new features for
constcode have been added, for example:- Floating point support
-
constimmediates for inline assembly - References to statics
- Mutable reference and pointers
Many new features for
unsafecode have been added, for example:- Strict provenance APIs
-
&rawpointer syntax - Safely addressing statics
-
Declaring safe items in unsafe
externblocks
-
The Cargo dependency resolver is now version aware. If a dependency crate specifies its minimum supported Rust version, Cargo uses this information when it resolves the dependency graph instead of using the latest
semver-compatible crate version.
Compatibility notes:
-
The WebAssembly System Interface (WASI) target is changed from
rust-std-static-wasm32-wasitorust-std-static-wasm32-wasip1. You can select the WASI target also by using the--target wasm32-wasip1parameter on the command line. For more information, see the Changes to Rust’s WASI targets upstream blog post. -
The split panic hook and panic handler arguments
core::panic::PanicInfoandstd::panic::PanicInfoare now different types. -
extern "C"functions abort the process on uncaught panics. Useextern "C-unwind"instead to allow unwinding across ABI boundaries.
Rust Toolset is a rolling Application Stream, and Red Hat only supports the latest version. For more information, see the Red Hat Enterprise Linux Application Streams Life Cycle document.