### Specify Example Target with Cargo Run Source: https://doc.rust-lang.org/cargo/print.html Use `--example` to specify which example to run with `cargo run`. ```bash cargo run --example ``` -------------------------------- ### Build all example targets Source: https://doc.rust-lang.org/cargo/print.html Compiles all example targets defined in the package. ```bash cargo rustc --examples ``` -------------------------------- ### Run an example with arguments Source: https://doc.rust-lang.org/cargo/print.html Executes a specific example within the package and passes additional arguments to it. The `--` separates Cargo arguments from example arguments. ```bash cargo run --example exname -- --exoption exarg1 exarg2 ``` -------------------------------- ### Run example with arguments Source: https://doc.rust-lang.org/cargo/commands/cargo-run.html Executes a specific example target, passing additional arguments to the binary after the -- separator. ```bash cargo run --example exname -- --exoption exarg1 exarg2 ``` -------------------------------- ### Publish Command Example Source: https://doc.rust-lang.org/cargo/commands/cargo-publish.html An example of how to publish the current package using Cargo. ```APIDOC ## Publish Command Example ### Description An example of how to publish the current package using Cargo. ### Example ``` __ cargo publish ``` ``` -------------------------------- ### Install a Rust Binary from a Crate Source: https://doc.rust-lang.org/cargo/print.html This command builds and installs a Rust binary crate from the crates.io registry. It installs the executable into the Cargo installation root's `bin` folder. ```bash cargo install ``` -------------------------------- ### Local File System Package ID Specifications Examples Source: https://doc.rust-lang.org/cargo/print.html Examples showing how to reference local packages on the filesystem using `file://` URLs, with and without version qualifiers. ```text Spec| Name| Version ---|---|--- `file:///path/to/my/project/foo`| `foo`| `*` `file:///path/to/my/project/foo#1.1.8`| `foo`| `1.1.8` `path+file:///path/to/my/project/foo#1.1.8`| `foo`| `1.1.8` ``` -------------------------------- ### Crates.io Package ID Specifications Examples Source: https://doc.rust-lang.org/cargo/print.html Examples demonstrating various ways to specify packages from crates.io, including different levels of version qualification. ```text Spec| Name| Version ---|---|--- `regex`| `regex`| `*` `regex@1.4`| `regex`| `1.4.*` `regex@1.4.3`| `regex`| `1.4.3` `https://github.com/rust-lang/crates.io-index#regex`| `regex`| `*` `https://github.com/rust-lang/crates.io-index#regex@1.4.3`| `regex`| `1.4.3` `registry+https://github.com/rust-lang/crates.io-index#regex@1.4.3`| `regex`| `1.4.3` ``` -------------------------------- ### Package Version Entry Example Source: https://doc.rust-lang.org/cargo/print.html A pretty-printed JSON example of a single line entry in a package file, detailing the format for published versions. ```json { // The name of the package. // This must only contain alphanumeric, `-`, or `_` characters. "name": "foo", // The version of the package this row is describing. // This must be a valid version number according to the Semantic // Versioning 2.0.0 spec at https://semver.org/. "vers": "0.1.0", ``` -------------------------------- ### Install a Package from Crates.io Source: https://doc.rust-lang.org/cargo/commands/cargo-install.html Installs a package and its dependencies from the crates.io registry. ```bash cargo install ripgrep ``` -------------------------------- ### Enable Rustdoc Scraping Examples Source: https://doc.rust-lang.org/cargo/reference/unstable.html Use this command to enable scraping examples from crates in the current workspace when generating documentation. ```bash cargo doc -Z unstable-options -Z rustdoc-scrape-examples ``` -------------------------------- ### Git Dependency Package ID Specifications Examples Source: https://doc.rust-lang.org/cargo/print.html Examples illustrating how to specify packages from Git repositories, including version tags, branches, and different URL formats. ```text Spec| Name| Version ---|---|--- `https://github.com/rust-lang/cargo#0.52.0`| `cargo`| `0.52.0` `https://github.com/rust-lang/cargo#cargo-platform@0.1.2`| `cargo-platform`| `0.1.2` `ssh://git@github.com/rust-lang/regex.git#regex@1.4.3`| `regex`| `1.4.3` `git+ssh://git@github.com/rust-lang/regex.git#regex@1.4.3`| `regex`| `1.4.3` `git+ssh://git@github.com/rust-lang/regex.git?branch=dev#regex@1.4.3`| `regex`| `1.4.3` ``` -------------------------------- ### Comparison Version Requirement Examples Source: https://doc.rust-lang.org/cargo/print.html Provides examples of comparison version requirements, allowing for precise control over the allowed version range. ```text >= 1.2.0 > 1 < 2 = 1.2.3 ``` -------------------------------- ### Install Rust and Cargo using rustup Source: https://doc.rust-lang.org/cargo/getting-started/installation.html Use this command to download and execute the rustup installation script on Linux and macOS. This will install the latest stable release of Rust, which includes Cargo. ```bash curl https://sh.rustup.rs -sSf | sh ``` -------------------------------- ### Install a Rust Binary from a Git Repository Source: https://doc.rust-lang.org/cargo/print.html This command allows you to build and install a Rust binary directly from a Git repository URL. You can specify a particular crate within the repository. ```bash cargo install --git https://github.com/user/repo.git ``` -------------------------------- ### Adding a Simple Dependency Source: https://doc.rust-lang.org/cargo/print.html Example of adding a dependency with just its version specified. ```toml [dependencies] rand = "0.7.3" ``` -------------------------------- ### Build specified example targets Source: https://doc.rust-lang.org/cargo/print.html Compiles specific example targets by name. Supports multiple specifications and glob patterns. Use quotes for glob patterns to prevent shell expansion. ```bash cargo rustc --example name ... ``` -------------------------------- ### Usage Examples Source: https://doc.rust-lang.org/cargo/commands/cargo-bench.html Common usage patterns for running benchmarks with Cargo. ```APIDOC ## Usage Examples ### Build and execute all benchmarks `cargo bench` ### Run a specific benchmark `cargo bench --bench bench_name -- modname::some_benchmark` ### Build benchmarks separately `cargo build --benches --release --keep-going` ``` -------------------------------- ### cargo run Source: https://doc.rust-lang.org/cargo/commands/cargo-run.html Executes the binary or example of the local package. ```APIDOC ## cargo run ### Description Run a binary or example of the local package. Arguments following '--' are passed directly to the binary. ### Method CLI Command ### Parameters #### Query Parameters - **--package** (string) - Optional - The package to run. - **--bin** (string) - Optional - Run the specified binary. - **--example** (string) - Optional - Run the specified example. - **--features** (string) - Optional - Space or comma separated list of features to activate. - **--all-features** (boolean) - Optional - Activate all available features. - **--no-default-features** (boolean) - Optional - Do not activate the default feature. - **--target** (string) - Optional - Run for the specified target architecture. - **--release** (boolean) - Optional - Run optimized artifacts with the release profile. - **--profile** (string) - Optional - Run with the given profile. - **--timings** (boolean) - Optional - Output compilation timing information. - **--target-dir** (string) - Optional - Directory for all generated artifacts. ``` -------------------------------- ### Cargo.toml Example Source: https://doc.rust-lang.org/cargo/print.html A basic Cargo.toml manifest for a package with a build script. ```toml [package] name = "hello-from-generated-code" version = "0.1.0" edition = "2024" ``` -------------------------------- ### Specify Toolchain Source: https://doc.rust-lang.org/cargo/commands/cargo-doc.html If Cargo is installed with rustup, arguments starting with `+` are interpreted as toolchain names (e.g., `+stable`, `+nightly`). ```bash +toolchain ``` -------------------------------- ### Credential Provider Error: Operation Not Supported Source: https://doc.rust-lang.org/cargo/print.html Sent when the credential provider does not support the requested operation. For example, if a provider only supports 'get' and a 'login' is requested. ```json { "Err": { "kind": "operation-not-supported" } } ``` -------------------------------- ### Specify Toolchain with +toolchain Source: https://doc.rust-lang.org/cargo/commands/cargo-add.html If Cargo is installed via rustup, arguments starting with `+` are interpreted as toolchain names (e.g., `+stable`, `+nightly`) for toolchain overrides. ```bash +_toolchain_ ``` -------------------------------- ### Example: Build Documentation with Custom CSS Source: https://doc.rust-lang.org/cargo/commands/cargo-rustdoc.html Demonstrates how to build documentation with custom CSS included from a file. ```APIDOC ## Example: Build Documentation with Custom CSS 1. Build documentation with custom CSS included from a given file: ``` cargo rustdoc --lib -- --extend-css extra.css ``` ``` -------------------------------- ### Disable Scraping Examples from a Specific Example Target Source: https://doc.rust-lang.org/cargo/print.html Set `doc-scrape-examples = false` for a specific `[[example]]` target to disable example scraping for that target. This is useful when you want to exclude certain examples from documentation. ```toml [[example]] name = "my-example" doc-scrape-examples = false ``` -------------------------------- ### List Installed Packages Source: https://doc.rust-lang.org/cargo/commands/cargo-install.html Displays a list of all packages that have been installed via `cargo install`. ```bash cargo install --list ``` -------------------------------- ### Enabling Build Timings Source: https://doc.rust-lang.org/cargo/print.html Example of enabling detailed build timing information using the --timings flag. ```bash cargo build --timings ``` -------------------------------- ### Customize Example Crate Type in Cargo.toml Source: https://doc.rust-lang.org/cargo/reference/cargo-targets.html Define the crate type for an example in Cargo.toml. This allows examples to be compiled as libraries. ```toml [[example]] name = "foo" crate-type = ["staticlib"] ``` -------------------------------- ### Configure Doc Scraping Examples in Cargo.toml Source: https://doc.rust-lang.org/cargo/reference/unstable.html Configure whether to scrape examples for library or specific example targets within your Cargo.toml file. Setting `doc-scrape-examples = true` for any example target enables scraping for that package. ```toml # Enable scraping examples from a library [lib] doc-scrape-examples = true # Disable scraping examples from an example target [[example]] name = "my-example" doc-scrape-examples = false ``` -------------------------------- ### Define Example Crate Type Source: https://doc.rust-lang.org/cargo/print.html Specify the crate type for an example to control how it's compiled. This is useful for examples intended to be used as libraries. ```toml __ [[example]] name = "foo" crate-type = ["staticlib"] ``` -------------------------------- ### View build script output Source: https://doc.rust-lang.org/cargo/reference/build-script-examples.html Example output from running cargo build -vv showing library discovery. ```text [libz-sys 0.1.0] cargo::rustc-link-search=native=/usr/lib [libz-sys 0.1.0] cargo::rustc-link-lib=z [libz-sys 0.1.0] cargo::rerun-if-changed=build.rs ``` -------------------------------- ### Running a Specific Benchmark Source: https://doc.rust-lang.org/cargo/print.html This example demonstrates how to run a specific benchmark named 'foo' and ensures an exact match, preventing 'foobar' from running. Arguments after '--' are passed to the benchmark binary. ```bash cargo bench -- foo --exact ``` -------------------------------- ### Example dep-info file content Source: https://doc.rust-lang.org/cargo/reference/build-cache.html Shows the Makefile-like syntax of a .d file indicating file dependencies for a build artifact. ```text /path/to/myproj/target/debug/foo: /path/to/myproj/src/lib.rs /path/to/myproj/src/main.rs ``` -------------------------------- ### Display dependency tree with feature details Source: https://doc.rust-lang.org/cargo/commands/cargo-tree.html This example shows a detailed dependency tree including feature information for the `syn` package and its dependencies. ```text syn v1.0.17 ├── syn feature "clone-impls" │ └── rustversion v1.0.2 │ └── rustversion feature "default" │ └── myproject v0.1.0 (/myproject) │ └── myproject feature "default" (command-line) ├── syn feature "default" (*) ├── syn feature "derive" │ └── syn feature "default" (*) ├── syn feature "full" │ └── rustversion v1.0.2 (*) ├── syn feature "parsing" │ └── syn feature "default" (*) ├── syn feature "printing" │ └── syn feature "default" (*) ├── syn feature "proc-macro" │ └── syn feature "default" (*) └── syn feature "quote" ├── syn feature "printing" (*) └── syn feature "proc-macro" (*) ``` -------------------------------- ### Enable Scraping Examples from a Library Source: https://doc.rust-lang.org/cargo/print.html Set `doc-scrape-examples = true` in the `[lib]` section to enable scraping examples from a library. This setting controls whether examples are included in the generated documentation. ```toml [lib] doc-scrape-examples = true ``` -------------------------------- ### Building with a Custom Profile Source: https://doc.rust-lang.org/cargo/reference/profiles.html Command to build a project using the custom 'release-lto' profile. ```bash cargo build --profile release-lto ``` -------------------------------- ### Build Script for Probing System Libraries Source: https://doc.rust-lang.org/cargo/print.html Use a build script (`build.rs`) with the `pkg-config` crate to probe for and configure linking to system libraries. This example probes for `zlib`. ```rust // build.rs fn main() { pkg_config::Config::new().probe("zlib").unwrap(); println!("cargo::rerun-if-changed=build.rs"); } ``` -------------------------------- ### Install Package from Current Directory Source: https://doc.rust-lang.org/cargo/commands/cargo-install.html Installs or reinstalls the package located in the current directory. ```bash cargo install --path . ``` -------------------------------- ### Default Version Requirement Examples Source: https://doc.rust-lang.org/cargo/print.html Illustrates how default version requirements are interpreted, showing the expanded range for various version formats. ```text 1.2.3 := >=1.2.3, <2.0.0 1.2 := >=1.2.0, <2.0.0 1 := >=1.0.0, <2.0.0 0.2.3 := >=0.2.3, <0.3.0 0.2 := >=0.2.0, <0.3.0 0.0.3 := >=0.0.3, <0.0.4 0.0 := >=0.0.0, <0.1.0 0 := >=0.0.0, <1.0.0 ``` -------------------------------- ### Cargo Exit Codes and Examples Source: https://doc.rust-lang.org/cargo/commands/cargo-check.html Information on Cargo's exit codes and usage examples. ```APIDOC ## Exit Codes * `0`: Cargo succeeded. * `101`: Cargo failed to complete. ## Examples 1. Check the local package for errors: ``` __ cargo check ``` 2. Check all targets, including unit tests: ``` __ cargo check --all-targets --profile=test ``` ``` -------------------------------- ### Successful Rust Installation Message Source: https://doc.rust-lang.org/cargo/getting-started/installation.html This message indicates that Rust and Cargo have been successfully installed on your system. ```text Rust is installed now. Great! ``` -------------------------------- ### Example Custom Build Script Source: https://doc.rust-lang.org/cargo/reference/build-scripts.html This script demonstrates how to use the `cc` crate to compile a C file and statically link it. It also includes a directive to rerun the build script if the source C file changes. ```rust // Example custom build script. fn main() { // Tell Cargo that if the given file changes, to rerun this build script. println!("cargo::rerun-if-changed=src/hello.c"); // Use the `cc` crate to build a C file and statically link it. cc::Build::new() .file("src/hello.c") .compile("hello"); } ``` -------------------------------- ### Pass Linker Flags to Examples Source: https://doc.rust-lang.org/cargo/print.html Pass the `-C link-arg=FLAG` option to the compiler only when building example targets. ```rust cargo::rustc-link-arg-examples=FLAG ``` -------------------------------- ### Build Standard Library with -Z build-std Source: https://doc.rust-lang.org/cargo/reference/unstable.html This example demonstrates how to compile the standard library from source using the `-Z build-std` flag with `cargo run`. It shows the output when the standard library is recompiled in debug mode. ```bash cargo new foo cd foo cargo +nightly run -Z build-std --target x86_64-unknown-linux-gnu ``` -------------------------------- ### Cargo Owner Command Examples Source: https://doc.rust-lang.org/cargo/commands/cargo-owner.html Examples demonstrating how to use the `cargo owner` command for various operations. ```APIDOC ## Examples ### List owners of a package ```bash __ cargo owner --list foo ``` ### Invite an owner to a package ```bash __ cargo owner --add username foo ``` ### Remove an owner from a package ```bash __ cargo owner --remove username foo ``` ### Using authentication token ```bash __ cargo owner --add newowner --token my_crate ``` ### Specifying a different registry ```bash __ cargo owner --registry my-registry --list my_crate ``` ``` -------------------------------- ### Build and Test with Keep-Going Flags Source: https://doc.rust-lang.org/cargo/commands/cargo-test.html Demonstrates building test binaries and running tests without stopping at the first failure. ```bash cargo build --tests --keep-going cargo test --tests --no-fail-fast ``` -------------------------------- ### Get Help for a Nested Cargo Command Source: https://doc.rust-lang.org/cargo/commands/cargo-help.html To get help for commands with subcommands, separate the command levels with spaces. ```bash cargo help report future-incompatibilities ``` -------------------------------- ### Filter and Run Tests with Options Source: https://doc.rust-lang.org/cargo/commands/cargo-test.html This example demonstrates how to filter tests by name and specify test runner options. Arguments after `--` are passed to the test binary. ```bash cargo test foo -- --test-threads 3 ``` -------------------------------- ### Future Incompatibility Warning Example Source: https://doc.rust-lang.org/cargo/print.html Example of a warning indicating that a package contains code incompatible with future Rust versions. ```text warning: the following packages contain code that will be rejected by a future version of Rust: rental v0.5.5 note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1` ``` -------------------------------- ### Cargo Configuration for Host Build Targets Source: https://doc.rust-lang.org/cargo/reference/unstable.html Example TOML configuration file demonstrating the 'host' key for specifying host build target settings like linker and runner. It includes both generic host settings and architecture-specific overrides. ```toml # config.toml [host] linker = "/path/to/host/linker" runner = "host-runner" [host.x86_64-unknown-linux-gnu] linker = "/path/to/host/arch/linker" runner = "host-arch-runner" rustflags = ["-Clink-arg=--verbose"] [target.x86_64-unknown-linux-gnu] linker = "/path/to/target/linker" ``` -------------------------------- ### CLI Command: cargo uninstall Source: https://doc.rust-lang.org/cargo/commands/cargo-uninstall.html Removes a package installed with cargo-install. Supports options to target specific binaries or installation roots. ```APIDOC ## cargo uninstall ### Description Removes a package installed with cargo-install(1). By default, all binaries for a crate are removed, but specific binaries can be targeted. ### Method CLI Command ### Parameters #### Options - **--package** (spec) - Optional - Package to uninstall. - **--bin** (name) - Optional - Only uninstall the specified binary name. - **--root** (dir) - Optional - Directory to uninstall packages from. - **--verbose** (flag) - Optional - Use verbose output. - **--quiet** (flag) - Optional - Do not print cargo log messages. - **--color** (when) - Optional - Control colored output (auto, always, never). ### Request Example ```bash cargo uninstall ripgrep ``` ### Response #### Success Response (0) - **Exit Code** (integer) - Cargo succeeded. #### Error Response (101) - **Exit Code** (integer) - Cargo failed to complete. ``` -------------------------------- ### Local Filesystem Package Specifications Source: https://doc.rust-lang.org/cargo/reference/pkgid-spec.html Examples of Package ID Specifications for local packages using 'file://' URLs. These demonstrate referencing local projects with or without specific versions. ```text Spec| Name| Version ---|---|--- `file:///path/to/my/project/foo`| `foo`| `*` `file:///path/to/my/project/foo#1.1.8`| `foo`| `1.1.8` `path+file:///path/to/my/project/foo#1.1.8`| `foo`| `1.1.8` ```