Compile Multiple Modules with RUSTC (Not Cargo)?

#1

So here I am. On my way to learning the basics of Rust where it seems I already have a complaint/problem. I think the problem stems from the lack of my ability to google the answer (I even found a link to “How to compile cargo without cargo” as funny to me as that was), but lets see what the community has to say.

Often while I was learning c/c++ (I still am) I found it really easy to just type out some lines of code and send it to the compiler to give me something to run. EG:
clang++ foo.cpp -o bar

Now I have been using this same method with the Rust compiler to provide me the same quick feedback. I have no problem using cargo, but it seems like the program has a default for meduim/large dev projects in mind right out of the gate. I have no real reason to use cargo to compile a 500 line or less piece of code it seems. I still don’t use many dependencies for my small projects, but when I build a project the folder containing the project balloons to well over 200 mb. With good reason, sure. I just feel there is no reason at this time for what I am doing…

So now to my question.

How the heck to we just use rustc to compile multiple modules into something I can run and test. I just want to run the command, let rustc chew on my code a little bit, and have it spit out a file.

Also, does rustc still depend on the main function lying in a file called main.rs, or can I specify:
rustc copy.rs
and have all the dependencies lie in copy.rs…
Do I still need a lib.rs in this case?

I understand the community really promoting cargo. I can see how useful it is, just not for me right now.

Any help would be appreciated.

#2

If your crate root is called copy.rs, you run rustc copy.rs.

#3

It would seem that simple IMHO.

Now lets say I have an additional module called foo.rs, in copy.rs I add the line
use foo;

I get back an error from the compiler saying:
warning: unused import: foo
–> copy.rs:12:5
|
12 | use foo;
| ^^^
|
= note: #[warn(unused_imports)] on by default

Maybe I should have clarified that in my original question. I thought I did, but guess not.

#4

Um, please take a closer look at that

#5

So clearly I’m just importing the module incorrect then? Seems simple enough.

Guess I’ll hit the book once again. Must have missed something simple

#6

To define a module, you have the following code:

//...
mod foo;
//...

and rustc will look for either:

src
├main.rs //or lib.rs
└foo.rs

or

src
├main.rs //or lib.rs
└foo
 └mod.rs

There were some newer rules introduced in Rust 2018, but I can’t recall them, but either way the above will work

#7

I guess to boil it down it is most simple form. Here is a screenshot of exactly where I am going wrong. I guess I just don’t understand for this evening. Rust certainly has me seriously qustioning my ability.

#8

No, no you have it wrong, in main.rs you just put

mod filename;

And this will tell the compiler that the module is in a file (or directory) named filename.rs and the contents of filename.rs should look like

pub struct Understand { //...

Rustc will only compile the files it needs to, and will ignore all files it doesn’t have a mod filename for.
You could also choose to use an explicit mod like what you thought you were supposed to use in your sub-file in your image
Sorry for the lack of better formatting or examples, mobile isn’t very comfortable

1 Like
#9

Oh my gosh. If that isn’t the supidest/most cleaver way of doing that syntax wise. I really appreciate your time OptimisticPeach. I think my main issue was, in my mind I was thinking of the file as a module. Not how the compiler would see it. This makes sense to me.

#10

You can run cargo build --verbose to learn how cargo does it.

Btw, cargo is a pretty thin layer if you don’t use dependencies, so it’s fine even for 1-line projects IMHO.

1 Like