[rust-dev] Modules and visibility

Matthias Einwag matthias.einwag at googlemail.com
Wed Jan 22 12:37:32 PST 2014

Hi everyone,

During my first steps with Rust (and working on this:
https://github.com/mozilla/rust/issues/11165) I realized that I had much
more trouble with the module and privacy system than in any other language
I worked with up to date. I really had no clear idea of how to structure my
source files and spent nearly ours about it. This seems a little
unfortunate to me, since I have the impression that a basic module and
visibility system doesn't have to be complicated.

There are basically two things that lead to this, and they are both quite

The first in is the relation between modules and files.
In the tutorial there is the sentence: "One important aspect about Rusts
module system is that source files are not important".
I absolutely can't agree on that one, because of the fact that with each
new file you also get new module.
I usually like to structure my programs into source code files where each
code file resembles a type or an object. Keep things small to get a
betteroview and can find the sources where things are declared faster.
However I also don't like Java which forces you to exactly create 1 source
file per object and arrange the folders according to the module/package
structure. In this regard I find Rust even worse to Java, because it's one
module per file.
If you like to have everything in a module (e.g. because the types in the
module tightly interact) this encourages you to simply put everything in a
single big source file. Or to give up on Rust.
Instead I really like to have something were files and types are really
loosely coupled, like C# and C++.

Putting several things in one module is often relevant for privacy reasons.
They tightly interact and I want to access priv fields or members of the
other items. Privacy is the second issue I have with the current system. In
my Rust project I gave up in several cases and just made things public to
allow types in other modules access to my types, even if it's really wrong
from an encapsulation point of view and also gives external library users
the opportunity to manipulate things. The alternative would be to give up
on splitted source files. C++ at least allow friend; and Java and C# an
internal visibility modifier to allow things to reference each without
giving up privacy to the outside.

Besides the problem that file->module leads to modified privacy settings I
also think it's quite verbose to do the whole imports for the file again.

To make things easier and better I would love to see two things in Rust:
1. A real splitting between source files and modules
   One solution is an alternative include to mod Otherfile; That would not
automatically create a new module and put the content of the file in it.
   So basically the same as C++ include. I know there's include!, but is
discouraged and from my tests also did not work for including types.
   The other option is to have something like Dart. There you have a top
level file for you module/library, where you specify which other files are
part of the module.
   Like `part "otherfile.rs";` In the other files you specify `part of
myModule;` to make clear that they belong to each other.
2. Crate local visibility
   I've seen that it was already discussed on the mailing list before. But
I also think that is a great benefit that enables one to access private
fields/methods of other types even though they are in different modules.
   I actually like the internal in C# more than the friend in C++, because
it's much easier to use. Rust could also introduce either an internal
visibility modifer in addition to the existing priv and pub ones.
   Or a crate setting that makes all priv items visible to all other types
in the crate. However I like the internal much better, because it's a more
fine grained and still easy-to-use control.

Don't get me wrong, I think Rust is a great language with lots of potential
- and some awesome developers.
But I think there are some corners were it is much harder to use than it
has to be. And the module system is one of them.

Best Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140122/c14a38c8/attachment.html>

More information about the Rust-dev mailing list