Is it possible to import a module into itself?

Today I’ve discovered this apparently valid program:

pub mod a {
    pub use super::*;
    pub fn a() {}
}

fn main() { a::a::a::a::a::a::a::a(); }

Are such “infinite” paths valid?

1 Like

The paths are not infinite. You’d need an infinite source file to make it so.

Apart from that, I could see a clippy lint against this. What do you think?

Challenge accepted!

$ (echo "pub mod a { use super::*; pub fn a() {} } fn main() {"; yes "a::"; echo "a(); }") | rustc -

But memory is not infinite, sadly… (Don’t OOM yourself trying this!)

3 Likes

Yep, this is valid. More generally, module paths are allowed to be cyclic.

2 Likes

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