I am trying to figure out how to build organized programs in Rust, and was curious about best practices about this. I have the following crate structure (binary):
main.rs
mod object;
use crate::object::sphere::Sphere as Sphere;
use object::Hitable;
fn main() {
let sphere = Sphere {
radius : 20.0
};
println!("Hello, world! {}", sphere.intersect());
}
This is the toy program setup I have right now. I had a few questions about this:
Am I doing the module import paradigm right? The Book only talks about organizing code by splitting it into multiple .rs files, but I did not seem to find any info on how to handle source files in multiple folders. Any tips and ideas would be appreciated!
In main.rs, it's rather difficult to use the Sphere struct, as in, the use line seems a bit long. Is there a better way to import and organize this?
This is a reasonable file layout, except I think most real multifile projects should be libs with a few scripts in src/bin. But for a toy this is absolutely fine.
You don't need as Sphere here, that is what you'd get without an alias.
Yes, it's there. Submodules must live in subdirs named after their parent modules. The file hierarchy necessarily matches the module hierarchy in Rust. You are doing it correctly.
I see! However, that declaration path seems rather long-winded. If I want to use a Sphere object inside main.rs is that the only way to do it given the current structure? Or is there some shorthand notation?
Also, if possible, could you please give me example of a bigger project that makes use of the libs+scripts in src/bin format so I can study them? Thanks!
Yes, you have to import it like that. When you have more to import from your own crate you can use nested paths to save a little typing. I think the only other option here is that your whole crate is automatically implicitly into main.rs, which would be terrible.
I can't think of an example off the top of my head - and my own projects are all private for now, but the crate layout is described here: Package Layout - The Cargo Book
This is an opinion rather than a hard rule, but I find that any script beyond a few hundred lines should be turned into a library that gets imported and called by a tiny script.