Usage Reference

Linux / macOS

# Install nightly driver (one-time)
cargo +nightly install cargo-slicer \
  --features rustc-driver \
  --bin cargo-slicer-rustc \
  --bin cargo_slicer_dispatch

# Pre-analyze workspace call graph (seconds)
cargo-slicer pre-analyze

# Build with virtual slicing
cargo clean
CARGO_SLICER_VIRTUAL=1 CARGO_SLICER_CODEGEN_FILTER=1 \
  RUSTC_WRAPPER=$(which cargo_slicer_dispatch) \
  cargo +nightly build --release

WSL on Windows drives (/mnt/c/, /mnt/d/, …)

Same as above but disable sccache to avoid NTFS permission errors:

SCCACHE_IDLE_TIMEOUT=0 cargo +nightly install cargo-slicer \
  --features rustc-driver \
  --bin cargo-slicer-rustc \
  --bin cargo_slicer_dispatch

cargo-slicer pre-analyze

cargo clean
CARGO_SLICER_VIRTUAL=1 CARGO_SLICER_CODEGEN_FILTER=1 \
  CARGO_SLICER_SCCACHE=/nonexistent \
  RUSTC_WRAPPER=$(which cargo_slicer_dispatch) \
  cargo +nightly build --release

Subcommands

SubcommandDescription
(default)Source slicing: copy deps, delete unused items
build [ARGS]Slice deps then build with sliced crates
pre-analyze [--parser BACKEND]Cross-crate static analysis for virtual slicing
generate [-o DIR] [--delete]Write a sliced source copy without modifying the original
script <file.rs> [args...]Run a nightly -Zscript single-file Rust script with slicing enabled
rl-bench [OPTIONS]Measure compile speedup as RL training KPIs

Pre-analysis parser backends

cargo-slicer pre-analyze                # syn (default, most accurate)
cargo-slicer pre-analyze --parser fast  # fast tokenizer
cargo-slicer pre-analyze --parser ctags # items only, no call edges
BackendSpeedCall edgesUse when
syn0.5–12 sYes, accurateDefault — best stubs
fast< 1 sYes, approximateLarge workspaces, time-sensitive
ctagsFastestNoneItems-only analysis

cargo-script (nightly -Zscript)

Run a single-file Rust script (cargo's nightly -Zscript feature) with cargo-slicer enabled.

Quick install (script subcommand only)

curl -fsSL https://raw.githubusercontent.com/yijunyu/cargo-slicer/main/install-script.sh | bash

This installs only the two binaries the script subcommand needs (cargo-slicer + cargo_slicer_dispatch, ~5 MB total). It skips the warmup cache, clang-daemon, and the nightly rustc-driver build that the full install.sh would set up.

After install, run cargo-slicer script --check to verify the setup.

Usage

The shebang line is the only thing that distinguishes a cargo-slicer script from a plain cargo-script:

#!/usr/bin/env cargo-slicer
---cargo
[dependencies]
regex = "1"
---

fn main() {
    let re = regex::Regex::new(r"\w+").unwrap();
    println!("{:?}", re.find("hello world"));
}

Make it executable and run it directly:

chmod +x hello.rs
./hello.rs

Or invoke explicitly:

cargo-slicer hello.rs [args...]
# or, equivalent, with the explicit subcommand form:
cargo-slicer script hello.rs [args...]

When cargo-slicer's first argument is an existing .rs file, it routes to the script subcommand automatically — so the shebang stays as short as a plain cargo-script shebang.

What it does

  1. Sets CARGO_SLICER_VIRTUAL=1, CARGO_SLICER_CODEGEN_FILTER=1, and CARGO_SLICER_CACHE_DIR=$TMPDIR/cargo-slicer-script-<hash> (keyed by the absolute script path, so caches don't litter the user's cwd).
  2. If the script's ---cargo frontmatter omits edition = "...", writes a patched copy into the cache dir with edition = "2024" injected so cargo-script doesn't emit the "no edition" warning.
  3. Detects whether the active nightly already has -Z dead-fn-elimination (the in-tree patch). If so, it skips RUSTC_WRAPPER and passes the flag via CARGO_ENCODED_RUSTFLAGS (Fast Path 3). Otherwise it sets RUSTC_WRAPPER=cargo_slicer_dispatch and uses the userspace driver.
  4. execs cargo +nightly -Zscript <file> [args...].

Caveats

  • -Zscript is unstable and only available on nightly.
  • For tiny single-file scripts the slicer's contribution is mostly eliminating dead code in registry dependencies (the userspace driver path skips the one-crate script body by the auto skip-threshold heuristic). The -Z dead-fn-elimination fast path applies to both.
  • No pre-analysis step runs — there's no workspace Cargo.toml to walk. The in-tree flag does its own BFS from the script's main(), so this is fine for the upstream-flag path.

Source slicing (stable, no nightly)

cargo-slicer                # slice all deps
cargo-slicer regex          # slice one crate
cargo-slicer --clean        # clean and re-slice
cargo-slicer -O             # fast production mode (skip verification)
cargo-slicer build --release  # slice + build

Optimization levels

LevelDescription
-O0No deletion — safe baseline
-O1Delete private functions (with verification)
-O2Delete all private items + trial deletion
-O3Graph-guided deletion (default)
-OFast production — skip verification