knh190
knh190

Reputation: 2882

How do I use Rust macros in the same crate?

I have the sources as:

src/
  main.rs
  memory.rs
  chunk.rs

memory.rs

I have a macro:

#[macro_export]
macro_rules! grow_capacity {
    ( $x:expr ) => {
        {
            if $x < 8 { 8 } else { $x * 2 }
        }
    };
}

chunk.rs

I want to use it:

use crate::memory;

// define struct Chunk

impl Chunk {
    pub fn write(&mut self, byte: u8) {
        // other lines of code.
        self.capacity = memory::grow_capacity!(self.capacity);
    }

It gives me error:

error[E0433]: failed to resolve: could not find `grow_capacity` in `memory`
  --> src/chunk.rs:27:28
   |
27 |             self.capacity = memory::grow_capacity!(self.capacity);
   |                                     ^^^^^^^^^^^^^ could not find `grow_capacity` in `memory`

To make the post short, I have the full code on Gist.

If I place the macro inside chunk.rs, then I can use it. I don't think How do I use a macro across module files? works. Has Rust changed its feature? I'm on Rust 1.44.1.

Upvotes: 0

Views: 53

Answers (0)

Related Questions