How to use crate identity in crate itself?

Like this:

cargo new foo
cd foo
echo "struct A; fn main() { let _ = foo::A; }" > foo/src/main.rs
cargo b

Then compile failed:

The purpose of it is that, I want to write a proc_macro in crate foo_macros, for all of user and crate foo use.

#[proc_macro]
pub fn a(input: TokenStream) -> TokenStream {
    (quote! {
        foo::A
    }).into()
}

it is OK when using in user crate, but failed when using in foo crate.

1 Like

You can add pub(crate) use self as foo; on the crate root.

not work.

But I found pub(crate) use crate as foo; can work.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.