Usage Reference
Virtual Slicing (recommended)
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
| Subcommand | Description |
|---|---|
| (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
| Backend | Speed | Call edges | Use when |
|---|---|---|---|
syn | 0.5–12 s | Yes, accurate | Default — best stubs |
fast | < 1 s | Yes, approximate | Large workspaces, time-sensitive |
ctags | Fastest | None | Items-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
- Sets
CARGO_SLICER_VIRTUAL=1,CARGO_SLICER_CODEGEN_FILTER=1, andCARGO_SLICER_CACHE_DIR=$TMPDIR/cargo-slicer-script-<hash>(keyed by the absolute script path, so caches don't litter the user's cwd). - If the script's
---cargofrontmatter omitsedition = "...", writes a patched copy into the cache dir withedition = "2024"injected so cargo-script doesn't emit the "no edition" warning. - Detects whether the active nightly already has
-Z dead-fn-elimination(the in-tree patch). If so, it skipsRUSTC_WRAPPERand passes the flag viaCARGO_ENCODED_RUSTFLAGS(Fast Path 3). Otherwise it setsRUSTC_WRAPPER=cargo_slicer_dispatchand uses the userspace driver. execscargo +nightly -Zscript <file> [args...].
Caveats
-Zscriptis 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-eliminationfast path applies to both. - No pre-analysis step runs — there's no workspace
Cargo.tomlto walk. The in-tree flag does its own BFS from the script'smain(), 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
| Level | Description |
|---|---|
-O0 | No deletion — safe baseline |
-O1 | Delete private functions (with verification) |
-O2 | Delete all private items + trial deletion |
-O3 | Graph-guided deletion (default) |
-O | Fast production — skip verification |