What does pub in a binary crate's root module?

I'm interested in knowing what effects pub has in a binary crate's root module. One effect I can observe is that unused items aren't reported as being unused. Also, rustdoc might be have differently, though I got the feeling that rustdoc behaves completely weird/errorneous when documenting a binary crate. But are there any other effects or is there any reason why to use pub (or not use pub) in a binary crate's root module?

2 Likes

I'm not certain, but I think it will have an effect on keeping code around in the final binary. You might want that for plugins, for example, if a symbol needs to be present for a library that you're going to dlopen.

That would usually be an

#[no_mangle]
pub extern "C" fn …

then? Or are there also cases without extern "C" where it makes sense? I.e. can you make binary plugins without the C ABI?

Probably #[no_mangle] or an explicit #[link_name = "..."], yeah. You could do it with the Rust ABI as long as you use the same compiler for everything, but a lazily linked/loaded plugin won't know the crate disambiguator for a mangled name.

In the other direction, there's a Rust ABI plugin example in the compiler itself with libraries providing __rustc_codegen_backend(), but I don't know if there's anything that actually provides symbols "forward" like I was supposing before.

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.