Revisiting Rust’s modules, part 2

I like most of this proposal a lot, much more than the previous one.

With a big but:

Relative paths

I really don’t like that paths are relative by default.

Almost all languages have absolute paths by default, and optional relative paths.

  • Python: from . import y (otherwise absolute, with Python 3)
  • Js: import "./relativeModule, otherwise absolute
  • Go: `import “./x”, otherwise absolute
  • Java: no relative imports at all
  • Haskell: also no relative imports (as far as I can remember)

So this would be a big break from the convention of other langauges.

What I would like to see:

Paths are absolute by default

All external crates also available as a regular path.

  • use std::collections::HashMap
  • type x = serde_json::Value

Referencing the current crate root is done by:

  • use cratename::mymodule
  • use ::mymodule (like it is now)

Relative paths

Since the . used by other languages is way too obtuse, I’d recommend an underscore.

So a leading underscore means a realtive path:

  • use _::submodule

  • type X = _::submodule::SomeType

  • type y = _::super::Whatever

    (just super without the leading underscore is NOT allowed, to make it unambiguous that relative paths start with an underscore)

This alleviates the concerns regarding origin ( crate-internal or external).

Everything is assumed external, unless the path starts with an underscore or with :: / the crate’s name.

It also stops the horrible confusion about paths (::x vs self::x vs x vs …).

3 Likes