λ€μ λ νμΌμ΄ μλ€κ³ κ°μ ν©λλ€.
// foo.rs
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro]
pub fn foo(x: TokenStream) -> TokenStream {
x
}
// bar.rs
extern crate foo;
use foo::foo;
fn main() {
foo!();
}
rustc foo.rs && rustc bar.rs -L.
λ₯Ό μ¬μ©νμ¬ μ΄λ¬ν νμΌμ μ»΄νμΌνλ €κ³ νλ©΄ λ€μ μ€λ₯κ° λ°μν©λλ€.
warning: proc macro crates and `#[no_link]` crates have no effect without `#[macro_use]`
--> bar.rs:1:1
|
1 | extern crate foo;
| ^^^^^^^^^^^^^^^^^
error[E0432]: unresolved import `foo::foo`
--> bar.rs:2:5
|
2 | use foo::foo;
| ^^^^^^^^ no `foo` in the root
error: cannot find macro `foo!` in this scope
--> bar.rs:5:5
|
5 | foo!();
| ^^^
error: aborting due to 2 previous errors
μ΄μ μ»΄νμΌλ¬μ μ‘°μΈμ λ°λ₯΄κ³ λμ #[macro_use]
λ₯Ό μλνλ©΄ λ€μ μ€λ₯κ° νμλ©λλ€.
error: procedural macros cannot be imported with `#[macro_use]`
--> bar.rs:5:5
|
5 | foo!();
| ^^^
|
= help: instead, import the procedural macro like any other item: `use foo::foo;`
error: aborting due to previous error
κ·Έλ¬λ κ·Έκ²μ λ΄κ° μ²μμ κ°μ§κ³ μλ κ²μ λλ€!
μ΄ λ¬Έμ μ λν μ€μ ν΄κ²°μ±
μ #![feature(use_extern_macros)]
λ₯Ό bar.rs
μ μΆκ°νλ κ²μ
λλ€. μ»΄νμΌλ¬λ ν¨μμ μ μ¬ν proc 맀ν¬λ‘λ₯Ό λ§λ λ λμ μ΄κ²μ μ μν΄μΌ ν©λλ€.
λ¨κΈ°μ μΌλ‘ #![feature(proc_macro)]
λ₯Ό bar.rs
μ μΆκ° νλλ‘ μ μνλλ‘ μ μ΄λ μ§λ¨ λ©μμ§λ₯Ό λ³κ²½νλ κ²μ μ¬μ΄ μμ μΌ μ μμ΅λλ€. (λλ μ μμ νκΈ° μ μ ν΄λΉ κΈ°λ₯μ΄ νμ±νλμ΄ μλμ§ λ¨Όμ μ§λ¨μ ν΅ν΄ νμΈνμμμ€.) μ€μ λ¬Έμ λ ν¬ν¨νλ ν μ μμ μ€λ³΅ λΆλΆμ΄ μμ΄λ μκ΄ μμ΅λλ€.
μ΄μ μΌκ°μ warning: this feature has been stable since 1.29.0. Attribute no longer needed
for #![feature(proc_macro)]
λ λ°μ΅λλ€.
κ·Έλ¬λ μ¬μ ν @lfairyμ λμΌν λ¬Έμ κ° μμ΅λλ€. μ΄μ 맀ν¬λ‘λ₯Ό μ¬μ©νλ €λ©΄ error: cannot find macro 'my_macro!' in this scope
λ₯Ό μ»μ΅λλ€. νμ§λ§ use my_crate::my_macro;
λ₯Ό μΆκ°νλ©΄ error[E0432]: unresolved import 'my_crate::my_macro'
κ° λ©λλ€.
μ΄μ proc_macro
κΈ°λ₯μ use_extern_macros
λ‘ κ΅μ²΄ν΄μΌ νλ κ² κ°μ΅λλ€ . Attribute is no longer needed
λ©μμ§μ μ μ©ν ννΈκ° λ κ²μ
λλ€.. ;)
μ¬κΈ°μ μλ λ§μ κΈ°λ₯μ΄ μμ νλμμΌλ―λ‘ μ΄κ²μ μ€λλμμ κ°λ₯μ±μ΄ μμΌλ―λ‘ λ«μ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
μ΄μ μΌκ°μ
warning: this feature has been stable since 1.29.0. Attribute no longer needed
for#![feature(proc_macro)]
λ λ°μ΅λλ€.κ·Έλ¬λ μ¬μ ν @lfairyμ λμΌν λ¬Έμ κ° μμ΅λλ€. μ΄μ 맀ν¬λ‘λ₯Ό μ¬μ©νλ €λ©΄
error: cannot find macro 'my_macro!' in this scope
λ₯Ό μ»μ΅λλ€. νμ§λ§use my_crate::my_macro;
λ₯Ό μΆκ°νλ©΄error[E0432]: unresolved import 'my_crate::my_macro'
κ° λ©λλ€.μ΄μ
proc_macro
κΈ°λ₯μuse_extern_macros
λ‘ κ΅μ²΄ν΄μΌ νλ κ² κ°μ΅λλ€ .Attribute is no longer needed
λ©μμ§μ μ μ©ν ννΈκ° λ κ²μ λλ€.. ;)