Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

cargo(1)

名稱

cargo — Rust 軟體包管理器

概要

cargo [options] command [args]
cargo [options] --version
cargo [options] --list
cargo [options] --help
cargo [options] --explain code

描述

This program is a package manager and build tool for the Rust language, available at https://rust-lang.org.

command may be one of:

命令

建置命令

cargo-bench(1)
    Execute benchmarks of a package.

cargo-build(1)
    編譯軟體包。

cargo-check(1)
    Check a local package and all of its dependencies for errors.

cargo-clean(1)
    Remove artifacts that Cargo has generated in the past.

cargo-doc(1)
    建置軟體包的文件。

cargo-fetch(1)
    Fetch dependencies of a package from the network.

cargo-fix(1)
    自動修復 rustc 回報的梳理警告。

cargo-run(1)
    Run a binary or example of the local package.

cargo-rustc(1)
    編譯軟體包,並傳遞額外選項給編譯器。

cargo-rustdoc(1)
    Build a package’s documentation, using specified custom flags.

cargo-test(1)
    Execute unit and integration tests of a package.

清單命令

cargo-add(1)
    Add dependencies to a Cargo.toml manifest file.

cargo-generate-lockfile(1)
    生成專案的 Cargo.lock

cargo-info(1)
    Display information about a package in the registry. Default registry is crates.io.

cargo-locate-project(1)
    Print a JSON representation of a Cargo.toml file’s location.

cargo-metadata(1)
    Output the resolved dependencies of a package in machine-readable format.

cargo-pkgid(1)
    Print a fully qualified package specification.

cargo-remove(1)
    Remove dependencies from a Cargo.toml manifest file.

cargo-tree(1)
    Display a tree visualization of a dependency graph.

cargo-update(1)
    Update dependencies as recorded in the local lock file.

cargo-vendor(1)
    Vendor all dependencies locally.

軟體包命令

cargo-init(1)
    在現有目錄中建立新的 Cargo 軟體包。

cargo-install(1)
    建置並安裝 Rust 二進位。

cargo-new(1)
    建立新的 Cargo 軟體包。

cargo-search(1)
    在 crates.io 中搜尋軟體包。

cargo-uninstall(1)
    移除 Rust 二進位。

發佈命令

cargo-login(1)
    Save an API token from the registry locally.

cargo-logout(1)
    Remove an API token from the registry locally.

cargo-owner(1)
    Manage the owners of a crate on the registry.

cargo-package(1)
    Assemble the local package into a distributable tarball.

cargo-publish(1)
    上傳軟體包到註冊表。

cargo-yank(1)
    Remove a pushed crate from the index.

Report Commands

cargo-report(1)
    Generate and display various kinds of reports.

cargo-report-future-incompatibilities(1)
    Reports any crates which will eventually stop compiling.

一般命令

cargo-help(1)
    顯示關於 Cargo 的幫助資訊。

cargo-version(1)
    顯示版本資訊。

選項

特殊選項

-V
--version

Print version info and exit. If used with --verbose, prints extra information.

--list

List all installed Cargo subcommands. If used with --verbose, prints extra information.

--explain code

Run rustc --explain CODE which will print out a detailed explanation of an error message (for example, E0004).

顯示選項

-v
--verbose

Use verbose output. May be specified twice for “very verbose” output which includes extra output such as dependency warnings and build script output. May also be specified with the term.verbose config value.

-q
--quiet

Do not print cargo log messages. May also be specified with the term.quiet config value.

--color when

Control when colored output is used. Valid values:

  • auto (default): Automatically detect if color support is available on the terminal.
  • always: Always display colors.
  • never: Never display colors.

May also be specified with the term.color config value.

清單選項

--locked

Asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will exit with an error when either of the following scenarios arises:

  • The lock file is missing.
  • Cargo attempted to change the lock file due to a different dependency resolution.

It may be used in environments where deterministic builds are desired, such as in CI pipelines.

--offline

Prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. With this flag, Cargo will attempt to proceed without the network if possible.

Beware that this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline.

May also be specified with the net.offline config value.

--frozen

Equivalent to specifying both --locked and --offline.

共用選項

+toolchain

If Cargo has been installed with rustup, and the first argument to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +nightly). See the rustup documentation for more information about how toolchain overrides work.

--config KEY=VALUE or PATH

Overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or provided as a path to an extra configuration file. This flag may be specified multiple times. See the command-line overrides section for more information.

-C PATH

Changes the current working directory before executing any specified operations. This affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering .cargo/config.toml, for example. This option must appear before the command name, for example cargo -C path/to/my-project build.

This option is only available on the nightly channel and requires the -Z unstable-options flag to enable (see #10098).

-h
--help

Prints help information.

-Z flag

Unstable (nightly-only) flags to Cargo. Run cargo -Z help for details.

環境

See the reference for details on environment variables that Cargo reads.

退出狀態

  • 0:Cargo 執行成功。
  • 101:Cargo 未能完成執行。

檔案

~/.cargo/
    Default location for Cargo’s “home” directory where it stores various files. The location can be changed with the CARGO_HOME environment variable.

$CARGO_HOME/bin/
    Binaries installed by cargo-install(1) will be located here. If using rustup, executables distributed with Rust are also located here.

$CARGO_HOME/config.toml
    The global configuration file. See the reference for more information about configuration files.

.cargo/config.toml
    Cargo automatically searches for a file named .cargo/config.toml in the current directory, and all parent directories. These configuration files will be merged with the global configuration file.

$CARGO_HOME/credentials.toml
    Private authentication information for logging in to a registry.

$CARGO_HOME/registry/
    This directory contains cached downloads of the registry index and any downloaded dependencies.

$CARGO_HOME/git/
    This directory contains cached downloads of git dependencies.

Please note that the internal structure of the $CARGO_HOME directory is not stable yet and may be subject to change.

範例

  1. Build a local package and all of its dependencies:

    cargo build
    
  2. Build a package with optimizations:

    cargo build --release
    
  3. Run tests for a cross-compiled target:

    cargo test --target i686-unknown-linux-gnu
    
  4. Create a new package that builds an executable:

    cargo new foobar
    
  5. Create a package in the current directory:

    mkdir foo && cd foo
    cargo init .
    
  6. Learn about a command’s options and usage:

    cargo help clean
    

BUGS

See https://github.com/rust-lang/cargo/issues for issues.

另請參見

rustc(1), rustdoc(1)