Is it possible to import a module into itself?

#1

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

#2

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?

0 Likes

#3

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

#4

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

2 Likes

closed #5

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

0 Likes