Build and test Rust applications
Use the Cargo build tool to create, build, test, and run Rust projects on Red Hat Enterprise Linux, including release builds and WebAssembly binaries.
Prerequisites Copy linkLink copied!
- Rust Toolset is installed.
Cargo project layout Copy linkLink copied!
The Cargo build tool uses conventions for directory structure and file placement within a Cargo package. Running the cargo new command generates the package structure and templates for a manifest and a project file. By default, it 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.
Create a Rust project Copy linkLink copied!
Create a new Rust project that follows Cargo conventions. For more information on Cargo conventions, see Cargo project layout.
Procedure Copy linkLink copied!
Create a Rust library project Copy linkLink copied!
Create a Rust library project by using the Cargo build tool (cargo new --lib).
Procedure Copy linkLink copied!
Build a Rust project Copy linkLink copied!
Build your Rust project by using the Cargo build tool. Cargo resolves all dependencies of your project, downloads missing dependencies, and compiles it using the rustc compiler.
Before you begin Copy linkLink copied!
About this task Copy linkLink copied!
By default, projects are built and compiled in debug mode. For information on compiling your project in release mode, see Build a Rust project in release mode.
Procedure Copy linkLink copied!
Build a Rust project in release mode Copy linkLink copied!
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.
Before you begin Copy linkLink copied!
About this task Copy linkLink copied!
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 Build a Rust project.
Procedure Copy linkLink copied!
Run a Rust program Copy linkLink copied!
In a Cargo project directory, run cargo run command to compile when needed and start your binary. The executable you launch matches your active debug or release profile.
Before you begin Copy linkLink copied!
Procedure Copy linkLink copied!
$ cargo run
If your program has not been built yet, Cargo builds your program before running it.
Test a Rust project Copy linkLink copied!
Test your Rust program by using the Cargo build tool. Cargo rebuilds your project and then runs the tests found in the project. By default, Rust projects are tested in debug mode.
Before you begin Copy linkLink copied!
About this task Copy linkLink copied!
By default, Rust projects are tested in debug mode.
Procedure Copy linkLink copied!
Test a Rust project in release mode Copy linkLink copied!
Test your Rust program in release mode by using the Cargo build tool. Cargo rebuilds your project and then runs the tests found in the project. Release mode optimizes your source code, which can increase compilation time while producing a binary that runs faster.
Before you begin Copy linkLink copied!
About this task Copy linkLink copied!
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.
Procedure Copy linkLink copied!
Compile code into a WebAssembly binary with Rust Copy linkLink copied!
Install the WebAssembly standard library to compile Rust code into WebAssembly binaries.