如何在另一个箱子中使用宏?

min*_*ree 11 macros rust rust-crates

我正在努力从我的生锈库中制作宏以供其他生锈项目使用.

这是我现在如何尝试这项工作的一个例子.

lib.rs:

#![crate_name = "dsp"]
#![feature(macro_rules, phase)]
#![phase(syntax)]

pub mod macros;
Run Code Online (Sandbox Code Playgroud)

macros.rs:

#![macro_escape]

#[macro_export]
macro_rules! macro(...)
Run Code Online (Sandbox Code Playgroud)

other_project.rs:

#![feature(phase, macro_rules)]
#![phase(syntax, plugin, link)] extern crate dsp;

macro!(...) // error: macro undefined: 'macro!'
Run Code Online (Sandbox Code Playgroud)

我是在正确的轨道上吗?我一直在尝试使用std :: macros作为参考,但我似乎没有太多运气.我有什么明显的遗失吗?

Chr*_*gan 7

你的属性纠缠不清.

#![…]外部范围,而#[…]指的是下一个项目.

以下是一些值得注意的事项:

  1. lib.rs,这#![feature(phase)]是不必要的,并且#![phase(syntax)]是没有意义的.

  2. other_project.rs,您的phase属性应用于,而不是extern crate dsp;项目 - 这就是为什么它不从它加载任何宏.删除!.

  • 为了生效,`#![feature]`必须在crate上,这意味着crate根文件,默认为`lib.rs`或`main.rs`,视情况而定. (2认同)