百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cargo-expand

> 包管理
开源

用于显示宏展开结果的子命令

3.1K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于显示宏展开结果的子命令

cargo-expand

安装后,以下命令会打印出应用于当前 crate 的宏展开和 #[derive] 展开的结果。

bash
$ cargo expand

这是对更冗长的编译器命令的封装:

bash
$ cargo rustc --profile=check -- -Zunpretty=expanded

安装

使用 cargo install cargo-expand 安装。

此命令可选地使用 [rustfmt] 来格式化展开后的输出。生成的代码通常比编译器直接输出的更易读。如果 rustfmt 不可用,展开的代码将不会被格式化。使用 rustup component add rustfmt 安装 rustfmt。

示例

$ cat src/main.rs

rust
#[derive(Debug)]
struct S;

fn main() {
    println!("{:?}", S);
}

$ cargo expand

…

选项

查看 cargo expand --help 以获取完整的选项列表,其中大部分与其他 Cargo 子命令一致。以下是在 cargo expand 上下文中常见的几个选项。

要展开特定的测试目标:

$ cargo expand --test test_something

要跳过 rustfmt 进行展开:

$ cargo expand --ugly

仅展开特定的模块、类型或函数:

$ cargo expand path::to::module

[![cargo expand punctuated::printing][punctuated.png]][syn] [![cargo expand token::FatArrow][fatarrow.png]][syn]

配置

cargo expand 命令会读取 $CARGO_HOME/config.toml 中的 [expand] 部分(如果存在的话,通常是 ~/.cargo/config.toml)。

使用 theme 设置来配置默认的语法高亮主题:

toml
theme = "TwoDark"

运行 cargo expand --themes 或 bat --list-themes 可打印可用主题列表。使用 theme = "none" 可禁用着色。

更改默认着色。

[rustfmt]: https://github.com/rust-lang/rustfmt [punctuated.png]: https://raw.githubusercontent.com/dtolnay/cargo-expand/screenshots/punctuated.png [fatarrow.png]: https://raw.githubusercontent.com/dtolnay/cargo-expand/screenshots/fatarrow.png [syn]: https://github.com/dtolnay/syn [expand]

Issues· 10 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustcargo

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类包管理
定价开源

> 相关工具

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image