Confused on mod, use, pub. Simple example


#1

I’ve reduced my program down to these 5 example files. What declarations do I need to add to module “funny” so that main.rs compiles as-is?

main.rs

mod funny;
use funny::{Funny, funny_a, funny_b};

fn main()
{
    funny_a(1000);
    funny_b(1000);
}

funny/mod.rs

pub struct Funny;

mod imports; // Must not be exposed to user
pub mod a;
pub mod b;

// [#1] what to put here

funny/a.rs

// [#2] what to put here

pub fn funny_a(val: u32) -> Funny
{
	funny_b(HAPPY_VALUE)
}

funny/b.rs

// [#3] what to put here

pub fn funny_b(val: u32) -> Funny
{
	funny_a(SAD_VALUE)
}

funny/import.rs

/* I want FFI imports in a separate file because they
are a mile long and shared by "a.rs" / "b.rs" */

pub const HAPPY_VALUE: u32 = 7;
pub const SAD_VALUE: u32 = 13;

#2
pub use self::a::funny_a;
pub use self::b::funny_b;
use funny::b::funny_b;
use funny::import::HAPPY_VALUE;
use funny::a::funny_a;
use funny::import::SAD_VALUE;

#3

Nope :confused:

Module a calls module b and vice-versa. That’s where my hangup is.


#4

Edited. Sorry; missed that.


#5

Awesome, that finally works, thank you!

Out of curiosity, why can’t I use a wildcard to just bring in everything from the “imports” module? (like the following)

funny/b.rs

use funny::Funny;
use funny::a::funny_a;
use funny::imports::*;

pub fn funny_b(val: u32) -> Funny
{
	funny_a(SAD_VALUE)
}

The documentation seems to state this should be possible.


#6

Well, you can… it’s just bad form. If you use a wildcard, it makes it harder to work out where any given symbol has come from.

About the only times I use a wildcard is when I’m re-exporting exactly one module (so it’s unambiguous), or I’m writing an enum member function that just matches on itself.


#7

Ahhh okay that makes sense.

It actually does give a compiler error though to use a wildcard there.

Thanks for your help.


#8

I should probably mention that wildcards are also buggy and sometimes freak out, especially in the presence of cyclic imports.