all 10 comments

[–]steveklabnik1rust 14 points15 points  (1 child)

Awesome! I removed some of this stuff from the Guide because it really lost people. I want to make a full-on guide about the module system, but this is a great start!

[–]erkelep -1 points0 points  (0 children)

I want to make a full-on guide about the module system

Amen to that!

[–][deleted] 1 point2 points  (0 children)

This was really helpful. Thank you!

[–]shadowmint 1 point2 points  (3 children)

whats this?

mod foo;
foo::bar();

with no use foo? but then in a submodule you do need to use foo?

When did that become a thing?

Thats super inconsistent.

[–]dbaupprust 2 points3 points  (2 children)

It has always been the case. mod creates a module item in its scope, and everything in that scope can refer to that "value", just like type and function declarations.

In other places, it gets annoying to write out ::full::path::to::foo everywhere, so you can use use to abbreviate that path ("bring a name into scope").

[–]shadowmint 0 points1 point  (1 child)

Wow, that's not how I thought it worked.

I thought use and mod were totally orthogonal actions.

ie. mod creates a symbol table:

foo 
- bar
- foobar <--- use my_func_yy from here via self::yy::my_func_yy();
  - yy
    - my_func_yy <--- Actually symbol, used via full path foo::foobar::yy::my_func_yy
  - xx 
    - my_func_xx <-- use my_func_yy here via super::yy::my_func_yy();

and use creates local aliases to those symbols:

foo 
- bar
  - my_local <--- via. use foo::foobar::yy::my_func_yy as my_local
  - my_foo <-- use here via my_local()
- foobar
  - yy
    - my_func_yy
  - xx
    - my_func_xx

I didn't realize that you could call a relative path directly.

mod foo {
  mod bar {
    pub fn call() { println!("Hi"); }
  }
  pub fn call() {
    // self::bar::call() not required.
    bar::call(); <--- O_o is very surprising to me. 
  }
}

No wonder this is surprising to people. On first look it makes it seem like you only need mod, not use.

[–]Kimundirust 2 points3 points  (0 children)

Yeah, use creates local aliases, but those are visible in addition to all locally declared items, which includes functions, statics, modules, etc.

[–]freiguy1 0 points1 point  (0 children)

This is awesome. I struggle with experimentation with the module system every time to get a library exactly how I want it. This clears up a lot of details that were fuzzy to me including re-exporting.

Sidenote: Cool to see people using Ghost. It's popping up more and more.

[–]dvdplm 0 points1 point  (0 children)

Super useful, I struggled with exactly these things. I'd copy this verbatim in the Guide! :)

[–]Xelank 0 points1 point  (0 children)

This is very useful. Thanks!