マクロではソースコードからソースコードの自動生成が行われる。
この生成内容を確認できると、いくらかデバッグが簡単になる。
以下を状況によって使い分けるとよい。
rust-analyzer にはこのための専用機能がある。
$crate
などの展開前のコードが混ざる事がある。
VS Code からの場合、以下の手順で実行できる。
以下のコードの vec!
の箇所で前述の操作を行うと…。
fn main() {
let words = vec!["Hello", "macro", "world"];
println!("{}", words.join(" "));
}
以下のような展開結果が表示される。
// Recursive expansion of vec! macro
// ==================================
(<[_]>::into_vec(
#[rustc_box]
$crate::boxed::Box::new(["Hello", "macro", "world"]),
))
cargo expand
コマンドも使える。
crates.io から cargo-expand
をインストールして cargo
を拡張する。
> cargo install cargo-expand
コマンドを実行する (以下は前述の例の main
関数を対象にした場合)。
> cargo expand main
以下のような展開結果が表示される。
fn main() {
let words = <[_]>::into_vec(
#[rustc_box]
::alloc::boxed::Box::new(["Hello", "macro", "world"]),
);
{
::std::io::_print(format_args!("{0}\n", words.join(" ")));
};
}