cargo Verified current stable Not installed? Build Tools

Cargo / Build Rustc Specific Package

Build Rustc Specific Package

Compile a specific package within a Rust project using rustc.

$
Terminal
cargo rustc -p <package>

When To Use

When working with multi-package projects and wanting tight control over build processes.

Pro Tip

Using the `--no-default-features` flag can help isolate package issues by not pulling in dependencies automatically.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
cargo rustc -p <package>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Finished dev [unoptimized + debuginfo] target(s) in 0.25s

   Building package `example_package v0.1.0` in `target/debug/examples`
   Compiling example_package v0.1.0 (path/to/example_package)
   Finished debug [unoptimized + debuginfo] target(s) in 1.20s

Anatomy of Output

Understanding the result

Compiling mypackage v0.1.0 (/path/to/mypackage) Package Compilation Notification

Shows which package is being compiled.

Finished release [optimized] target(s) in 1.12s Completion Status

Indicates successful completion of the specific package build.

Documenting mypackage v0.1.0 Documentation Generation

Verifies documentation generation aligns with package build.

Power User Variants

Optimized versions

cargo rustc --package mypackage --release

Compile the specific package in release mode.

cargo rustc --package mypackage --no-default-features

Compile while disabling automatic dependencies.

Troubleshooting

Common pitfalls

error: package `mypackage` not found

Solution: Check the package name for correctness in the `Cargo.toml`.

error: cannot find the package specified with the `--package` flag

Solution: Ensure the specified package is declared in the workspace.

error: building package requires features not enabled

Solution: Verify necessary features are enabled in the command.

Command Breakdown

What each part is doing

cargo
Base Command
The executable that performs this operation. Here it runs Cargo before the shell applies any redirect operators.
-p
p| package
The value supplied for p| package.
<package>
package
The value supplied for package.
-p
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: `cargo rustc --package example_package` to build the specified package.

  2. Step 2

    Verify the build output in the `target/debug/examples` directory for errors or warnings.

Alternative Approaches

Comparable commands in other tools

Alternative build tools tools for the same job.