이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 2. The Cargo build tool


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

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

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

  1. Create a Rust project:

    $ cargo new --bin <project_name>
    Copy to Clipboard Toggle word wrap

    Replace <project_name> with your project name.

  2. To edit the project code, edit the main executable file main.rs and add new source files to the src subdirectory.

2.3. Creating a Rust library project

Complete the following steps to create a Rust library project by using the Cargo build tool.

Procedure

  1. Create a Rust library project:

    $ cargo new --lib <project_name>
    Copy to Clipboard Toggle word wrap

    Replace <project_name> with the name of your Rust project.

  2. To edit the project code, edit the src/lib.rs source file.

2.4. Building a Rust project

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

  1. In the Rust project directory, build the project:

    $ cargo build
    Copy to Clipboard Toggle word wrap
  2. To verify that your Rust program can be built when you do not need to build an executable file, enter:

    $ cargo check
    Copy to Clipboard Toggle word wrap

2.5. Building a Rust project in release mode

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

  1. In the Rust project directory, build the project in release mode:

    $ cargo build --release
    Copy to Clipboard Toggle word wrap
  2. To verify that your Rust program can be built when you do not need to build an executable file, enter:

    $ cargo check
    Copy to Clipboard Toggle word wrap

2.6. Running a Rust program

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
    Copy to Clipboard Toggle word wrap

    If your program has not been built yet, Cargo builds your program before running it.

2.7. Testing a Rust project

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

  1. Add the #[test] attribute in front of your function.
  2. Enter in the project directory:

    $ cargo test
    Copy to Clipboard Toggle word wrap

2.8. Testing a Rust project in release mode

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

  1. Add the #[test] attribute in front of your function.
  2. Enter in the project directory:

    $ cargo test --release
    Copy to Clipboard Toggle word wrap

2.9. Configuring Rust project dependencies

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

  1. In your project directory, edit the Cargo.toml file, and list each dependency in the following format in the [dependencies] section:

    <crate_name> = <version>
    Copy to Clipboard Toggle word wrap

    Rust code packages are called crates.

  2. Rebuild your project:

    $ cargo build
    Copy to Clipboard Toggle word wrap
  3. Run your project:

    $ cargo run
    Copy to Clipboard Toggle word wrap

2.10. Building documentation for a Rust project

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.

Procedure

  1. In your code, use three slashes /// at the beginning of a line to mark the line for extracting the comment for documentation.
  2. Build the documentation:

    $ cargo doc --no-deps
    Copy to Clipboard Toggle word wrap

    The command stores the generated documentation in the .target/doc/ directory.

2.11. Compiling code into a WebAssembly binary with Rust

Complete the following steps to install the WebAssembly standard library.

Procedure

  1. Install the WebAssembly standard library:

    • On RHEL 8, enter:

      # yum install rust-std-static-wasm32-unknown-unknown
      Copy to Clipboard Toggle word wrap
    • On RHEL 9 and 10, enter:

      # dnf install rust-std-static-wasm32-unknown-unknown
      Copy to Clipboard Toggle word wrap
  2. Use WebAssembly with Cargo:

    $ cargo <command> --target wasm32-unknown-unknown
    Copy to Clipboard Toggle word wrap

    Replace <command> with the Cargo command you want to run.

2.12. Vendoring Rust project dependencies

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.

Procedure

  • To vendor your Rust project with dependencies by using Cargo, enter in the project directory:

    $ cargo vendor
    Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat