Any plans to support multiple files in the rust playground?

Hello,
I was wondering if Rust playground would support multiple files sometime soon. Is this on the roadmap?
Thanks,

  • ndrewxie
1 Like

Can you expand on what your goals are? Most of the time, people want to create modules. You can just do these inline instead:

mod inner {
    pub struct Foo;
}

fn main() {
    inner::Foo;
}
4 Likes

Yeah, there’s that, but I’ve always thought that was a “hack”, and it’s a little less convenient (you have to merge all files together, make changes, then unmerge). Also, things like #[test] aren’t supported yet, sadly.

What do you mean by that? An inline module behaves exactly identically to a separate file module.

2 Likes

I have sometimes felt a similar need, when having worked on an example project (thus possibly making it multi-file yadda yadda), and then wanting to share a run of the program / a compilation message with people; and the playground is obviously not up for the task.

I agree it may be too situational compared to the effort that it may require. Imho, a tool that “unifies” a cargo project into a single file should be easier to make and could allow to solve this issue (for small-sized projects).

Can you expand on this? #[test] is certainly supported:

fn main() {}

mod test {
    #[test]
    fn run_me() {}
}

(Playground)

Output:


running 1 test
test test::run_me ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out


Errors:

   Compiling playground v0.0.1 (/playground)
warning: function is never used: `main`
 --> src/lib.rs:1:1
  |
1 | fn main() {}
  | ^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.94s
     Running target/debug/deps/playground-8ea77c1150429ae3
   Doc-tests playground

3 Likes

In a hypothetical world where the playground has multiple files, then you’d still need to do the work of copy and pasting each original file, creating a corresponding playground file. Creating the playground file requires specifying the right path for the file, meeting or exceeding any work needed to replace mod foo; with mod foo {}.

I can also guarantee that as soon as such a feature was added, people would ask how to use it to create files outside of the src directory.

Honestly, such a tool would be useful beyond just the playground. Bug reports would probably benefit from it as well. (Now I’m wondering if it could be super fancy and be extended to reduce code as well…)

3 Likes

rust-bundler looks very much like it. And its original purpose (a coding competition site) is not very surprising either :slight_smile:

It might need an update for the 2018 edition, I haven’t tested it…

3 Likes

Oh, wow, I didn’t know that! Thanks a lot!