# Anonymous modules

**URL:** <https://internals.rust-lang.org/t/anonymous-modules/15441>\
**Category:** compiler\
**Created:** [October 12, 2021, 5:59pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441 "2021-10-12T17:59:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [October 12, 2021, 5:59pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/1 "2021-10-12T17:59:21Z")

</div>

What if this syntax was allowed:

```rust
mod _ {
   /* implementantions */
}

```

This could be useful for proc-macro authors, as they can use the namespacing for hygiene. For example, they would be able to create a helper structs without potentially polluting the namespace, or exposing them to the user. As far as I know there aren't any drawbacks, but there may not be such a big need for such a thing.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [October 12, 2021, 6:17pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/2 "2021-10-12T18:17:42Z")

</div>

How would you name anything inside of such a module from outside? If you don’t need the ability to name anything inside of the module, you can already use

```rust
const _: () = {
    // definitions, trait impls, etc…
};

```

---

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [October 12, 2021, 6:48pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/3 "2021-10-12T18:48:37Z")

</div>

> [@steffahn](#):
>
> How would you name anything inside of such a module from outside? If you don’t need the ability to name anything inside of the module, you can already use

Well I didn' know this was possible. As I conceived it, this would be no different to that.

---

<div class="post-metadata">

**Author:** ![pdolezal](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pdolezal/32/7137_2.png) [@pdolezal](https://internals.rust-lang.org/u/pdolezal)\
**Post date:** [October 13, 2021, 8:22pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/4 "2021-10-13T20:22:20Z")

</div>

I was scratching my head for a while before I understood it. Nice! But I'd prefer `mod _` more, because that was clear to me immediately. However, as you mentioned, I'm missing the point how that could be used for anything useful 🤔

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [October 13, 2021, 8:29pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/5 "2021-10-13T20:29:52Z")

</div>

> [@pdolezal](#):
>
> However, as you mentioned, I'm missing the point how that could be used for anything useful 🤔

Oh, no, for sure there's a point. I didn't want to suggest otherwise. This pattern can be useful for creating _trait implementations_; you can define some structs, types, functions that you're going to use internally in the trait impl in that module (this is particularly useful if you want to use some generated struct as an _associated type_ for the trait implementation) - no need to be able to name anything inside of the module from outside of the module.

Note that this pattern is indeed already sometimes used in macros that generate trait implementations. I know about it from seeing it in some crate; unfortunately at the moment I don't quite remember which crate that was.

_Edit:_ Ah, here's one: [`serde`](https://github.com/serde-rs/serde) uses the pattern for their derive macros. E.g. click "Tools-\>Expand Macros" in their [example playground](https://play.rust-lang.org/?edition=2018&gist=72755f28f99afc95e01d63174b28c1f5).

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [October 14, 2021, 12:37am UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/6 "2021-10-14T00:37:29Z")

</div>

I would really like `mod {...}` to be a "module expression" that desugars to a named (though the name is inaccessible) module defined somewhere else and just referred. Not sure if I explained it correctly, but it'll be easier to explain with an example:

```rust
use mod {
    pub struct Foo;
    pub struct Bar;
    pub struct Baz
}::{Foo, Bar};

```

This will bring `Foo` and `Bar` into scope, with `Baz` only accessible inside the module because even though it is public - the module cannot be named.

Of course - that part is not very useful. But consider this:

```rust
fn foo() -> mod {
    pub struct Foo {
        // ...
    }

    impl Foo {
        // ...
    }
}::Foo {
    // function body
}

```

Which is also not super useful - unless used in a macro:

```rust
fn foo() -> make_struct!(/* data used for creating Foo */) {
    // ...
}

```

---

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [October 14, 2021, 2:28am UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/7 "2021-10-14T02:28:56Z")

</div>

That's an interesting idea. I think that the current `const _: () = {/* ... */};` hack covers most use cases, but the ability to export functions can be useful for some corner cases. I imagine that it can be used to avoid shadowing statics among other things.

---

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [October 19, 2021, 3:48pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/8 "2021-10-19T15:48:14Z")

</div>

Ok so one difference between `const _: () = {/* ... */ }` and `mod _ { /* ... */ }` would be that inside mod the namespace would be completely empty, so something like this would not throw an error:

```rust
const pi: f64 = 3.14; 
mod _ {
   fn foo() {
        let pi = 22.0/7.0; 
   }
}

```

while this does:

```rust
const pi: f64 = 3.14; 
const _: () = {
   fn foo() {
        let pi = 22.0/7.0; 
   }
}; 

```

Other than that there would be no difference

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [October 19, 2021, 5:26pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/9 "2021-10-19T17:26:01Z")

</div>

Good point, totally missed that! However, realistically in the application of writing macros, I'd assume you'll most often actually _want_ to be able to access everything that is in scope in the surrounding scope. Adding `use super::*` helps in this case though. Actually, it's not quite the same, as that only imports things from the surrounding _module_, not everything in-between, e.g. items defined in the surrounding block in a function or a const.

I guess we're actually _lacking_ proper support for importing "every item that's in scope" from where a module is defined inside of that module.

Example

```rust
fn f() {
    struct S;
    const _: () = {
        fn foo() -> S { S } // works
    };
    mod m {
        use super::*; // <- we might want an alternative
        // to `use super::*` that DOES make the following work
        fn foo() -> S { S } // doesn't work
    };
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 17, 2022, 5:26pm UTC](https://internals.rust-lang.org/t/anonymous-modules/15441/10 "2022-01-17T17:26:58Z")

</div>

Closed on January 17, 2022, 5:26pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
