Question: I am working on a certain problem where given a URL I need to map it to a country. Even if I can reliably answer the question "Is this particular URL relevant to United States?" that should be sufficient. 

I am happy to have false negatives but never a false positive. 

Currently I am considering the following approach.

* See if the domain name of the URL is in top 1000 .com domains on Alexa for USA.
* Check if the the link was shared by US users on Facebook or twitter.

Has anyone solved this kind of problem before ?

Answer: 
Macros within the same crate
============================

New method (since Rust 1.32, 2019-01-17)
----------------------------------------

```
foo::bar!();  // works

mod foo {
    macro_rules! bar {
        () => ()
    }

    pub(crate) use bar;    // <-- the trick
}

foo::bar!();  // works

```

With the `pub use`, the macro can be used and imported like any other item. And unlike the older method, this does not rely on source code order, so you can use the macro before (source code order) it has been defined.

Old method
----------

```
bar!();   // Does not work! Relies on source code order!

#[macro_use]
mod foo {
    macro_rules! bar {
        () => ()
    }
}

bar!();    // works

```

If you want to use the macro in the same crate, the module your macro is defined in needs the attribute `#[macro_use]`. Note that macros can only be used **after** they have been defined!

---

Macros across crates
====================

**Crate `util`**

```
#[macro_export]
macro_rules! foo {
    () => ()
}

```

**Crate `user`**

```
use util::foo;

foo!();

```

Note that with this method, macros always live at the top-level of a crate! So even if `foo` would be inside a `mod bar {}`, the `user` crate would still have to write `use util::foo;` and *not* `use util::bar::foo;`. By using `pub use`, you can export a macro from a module of your crate (in addition to it being exported at the root).

Before Rust 2018, you had to import macro from other crates by adding the attribute `#[macro_use]` to the `extern crate util;` statement. That would import all macros from `util`. This syntax should not be necessary anymore.
I have [came across the same problem](https://stackoverflow.com/questions/63233545/how-do-i-use-rust-macros-in-the-same-crate) in Rust 1.44.1, and this solution works for later versions (known working for Rust 1.7).

Say you have a new project as:

```
src/
    main.rs
    memory.rs
    chunk.rs

```

In **main.rs**, you need to annotate that you are importing macros from the source, otherwise, it will not do for you.

```rust
#[macro_use]
mod memory;
mod chunk;

fn main() {
    println!("Hello, world!");
}

```

So in **memory.rs** you can define the macros, and you don't need annotations:

```rust
macro_rules! grow_capacity {
    ( $x:expr ) => {
        {
            if $x < 8 { 8 } else { $x * 2 }
        }
    };
}

```

Finally you can use it in **chunk.rs**, and you don't need to include the macro here, because it's done in main.rs:

```rust
grow_capacity!(8);

```

The [upvoted answer](https://stackoverflow.com/a/31749071/3340588) caused confusion for me, with [this doc by example](https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute), it would be helpful too.

---

**Note:** This solution does work, but do note as @ineiti highlighted in the comments, **the order u declare the `mod`s in the `main.rs/lib.rs` matters**, all `mod`s declared after the macros mod declaration try to invoke the macro will fail.