如何在仅使用 rustc 进行代码分析的现有项目中获得与 Cargo 类似的依赖项解析?

How can I get similar dependency resolution to Cargo in an existing project with only rustc for code analysis?

Rust 书中提到 macros can be expanded 与命令 rustc --pretty expanded。我想用它来测试我在板条箱中编写的一些宏,方法是使用

之类的命令扩展示例文件
rustc -Z unstable-options --pretty expanded examples/macro_test.rs

macro_test.rs 的代码如下所示:

#[macro_use] extern crate macro_crate;

use macro_crate::macros::*;

macro_foo! { foo }

fn main() {}

然而,这会导致错误 0463,即 rustc 对它所在的板条箱环境一无所知:

error[E0463]: can't find crate for `macro_crate`
 --> examples/macro_test.rs:1:1
  |
1 | extern crate macro_test;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

推荐的处理方法是什么?据我所知,cargo 帮助没有任何直接相关的内容。

Cargo 有一个 rustc 子命令,用于调用带有附加参数的 rustc

$ cargo rustc --example macro_test -- -Z unstable-options --pretty expanded

您还可以在 -- 之前添加 --verbose 让 Cargo 打印完整的 rustc 命令行(以及其他内容)。