0

I have a crate with main.rs and types.rs. types.rs is used as mod types; in main.rs.

I'm designing types.rs as a library which contains functions/fields that may not be used by main.rs, but the compiler gives me uncountable warnings about something "is never used".

What is the correct solution here?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Magicloud
  • 818
  • 1
  • 7
  • 17

1 Answers1

2

A solution could be to create a lib.rs at the same level as your main.rs which will become your crate which you would be able to use things from. The mod types; should go into the lib.rs and then you can pick the things you'd like to be available for the outside world via pub use.

Inside a module, if something is not marked as pub then it has to be used -- hence the warning.

You could temporarily disable warnings while you are developing.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
  • Thanks. Does it have to be named "lib.rs"? By the second paragraph, I did have all fn/struct prefixed with pub, which I thought to be the way to work. But I got the warning. – Magicloud May 06 '19 at 03:56
  • Well, yes, there are a few special naming conventions in rust, such as `main.rs` for executable programs, `lib.rs` for crates, and `mod.rs` to turn directories into modules. Are you still using stuff from your `types.rs` directly or from your crate? – Peter Varo May 06 '19 at 06:52
  • I have to do: mod types; pub use types::*; in lib.rs to make it work.... Strange. – Magicloud May 06 '19 at 07:04
  • not strange at all, that's exactly how it supposed to work. you are creating a crate. a crate is a "super module" or "module of modules". your types module is just part of that crate. your executable will be using your crate. It all make perfect sense, don't you think? – Peter Varo May 06 '19 at 07:09
  • (on a side note: you probably don't want to do `pub use types::*;` global "exports" are not necessarily the things you want to have, you could/should be more specific there) – Peter Varo May 06 '19 at 07:11
  • I see. Just not quite used to controlling what is exposed to crate and what is exposed to outside by the same "pub". – Magicloud May 06 '19 at 07:25