Questo contenuto non è disponibile nella lingua selezionata.
Chapter 2. The Cargo build tool
Cargo is Rust’s build tool, package manager, and front end for the rustc compiler. After you declare dependency versions in your manifest, you can use Cargo to fetch crates, build the project, and run tests.
2.1. The Cargo directory structure and file placements Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
Create a new Rust project that is set up according to the Cargo conventions. The cargo new command generates the package directory structure, a manifest file, and initial source files.
Procedure
Create a Rust project:
$ cargo new --bin <project_name>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 Copia collegamentoCollegamento copiato negli appunti!
Create a Rust library project by using the Cargo build tool with the cargo new --lib command. The result is a package layout suitable for a library rather than a binary.
Procedure
Create a Rust library project:
$ cargo new --lib <project_name>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 Copia collegamentoCollegamento copiato negli appunti!
Build your Rust project by using the Cargo build tool. Cargo resolves all dependencies of your project, downloads missing dependencies, and compiles it by 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 buildTo verify that your Rust program can be built when you do not need to build an executable file, enter:
$ cargo check
2.5. Building a Rust project in release mode Copia collegamentoCollegamento copiato negli appunti!
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 --releaseTo verify that your Rust program can be built when you do not need to build an executable file, enter:
$ cargo check
2.6. Running a Rust program Copia collegamentoCollegamento copiato negli appunti!
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.
Prerequisites
Procedure
To run a Rust program managed as a project by Cargo, enter in the project directory:
$ cargo runIf your program has not been built yet, Cargo builds your program before running it.
2.7. Testing a Rust project Copia collegamentoCollegamento copiato negli appunti!
Test your Rust program by 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
2.8. Testing a Rust project in release mode Copia collegamentoCollegamento copiato negli appunti!
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
2.9. Configuring Rust project dependencies Copia collegamentoCollegamento copiato negli appunti!
Configure the dependencies of your Rust project by using the Cargo build tool. To specify dependencies for a project managed by Cargo, edit the Cargo.toml file in the project directory and rebuild your project. Cargo downloads the Rust code packages and their dependencies and stores them locally. Use cargo build or cargo run commands to compile your code with those packages and produce a runnable 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>Rust code packages are called crates.
Rebuild your project:
$ cargo buildRun your project:
$ cargo run
2.10. Building documentation for a Rust project Copia collegamentoCollegamento copiato negli appunti!
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-depsThe command stores the generated documentation in the
target/doc/directory.
2.11. Compiling code into a WebAssembly binary with Rust Copia collegamentoCollegamento copiato negli appunti!
Install the WebAssembly standard library to compile Rust code into WebAssembly binaries. You can then build and run Rust projects that target the WebAssembly platform.
Prerequisites
Procedure
Install the WebAssembly standard library:
On RHEL 8, enter:
# yum install rust-std-static-wasm32-unknown-unknownOn RHEL 9 and 10, enter:
# dnf install rust-std-static-wasm32-unknown-unknown
Use WebAssembly with Cargo:
$ cargo <command> --target wasm32-unknown-unknownReplace
<command>with the Cargo command you want to run.
2.12. Vendoring Rust project dependencies Copia collegamentoCollegamento copiato negli appunti!
Create a local copy of the dependencies of your Rust project for offline redistribution by using the Cargo build tool (vendoring). Vendored sources live in the vendor directory, including any Windows-target crates you captured. Point Cargo at that tree to build offline without network access.
Prerequisites
Procedure
To vendor your Rust project with dependencies by using Cargo, enter in the project directory:
$ cargo vendor