tamuhey
tamuhey

Reputation: 3535

How to bundle source files into one file?

I created a Rust binary project with cargo new --bin, and ended up with multiple source files.

However, Coursera only accepts single source file solutions.

How can I bundle all project files into one main.rs file?

Upvotes: 0

Views: 880

Answers (2)

trent
trent

Reputation: 28075

In Rust, every file is a module. But that doesn't mean that every module needs its own file.

I created a Rust binary project with cargo new --bin, and ended up with multiple source files.

cargo new --bin creates only one source file, src/main.rs. If you created some other .rs file, you must have put a mod declaration so you could use it inside main.rs:

// main.rs
mod foobar;
use foobar::Foo;
// foobar.rs
struct Foo {}

But instead of creating a separate file, you can put the contents directly in main.rs by changing the mod line:

// main.rs
mod foobar {
    struct Foo {}
}
use foobar::Foo; // works exactly the same

Upvotes: 2

Shepmaster
Shepmaster

Reputation: 432089

You can use a tool like rust-sourcebundler.

Upvotes: 3

Related Questions