I'm trying to grok the appropriate way to split up my binary project into multiple files. In the Rust book, 7.1 Packages and Crates starts off by including an example of creating a bin
project with cargo, but the rest of the examples are lib
. I don't really care about the hierarchy of my code since I'm not exposing it in a library. Other, older questions (for example) mention that you generally need <module_name>/mod.rs
; while this isn't strictly required, it does seem appropriate for larger library projects.
I have three files:
src/main.rs:
mod A;
mod B;
fn main() {
let a = A::A { a: 123 };
let b = B::B { b: 3.14 };
println!("{:?}", a);
println!("{:?}", b);
}
src/A.rs:
use crate::B;
#[derive(Debug)]
pub struct A {
pub a : u32
}
impl From<B::B> for A {
fn from(b : B::B) -> A {
A { a: b.b as u32 }
}
}
src/B.rs:
use crate::A;
#[derive(Debug)]
pub struct B {
pub b : f32
}
impl From<A::A> for B {
fn from(a : A::A) -> B {
B { b: a.a as f32 }
}
}
This works as expected (snake case warnings removed for brevity):
$ cargo run
A { a: 123 }
B { b: 3.14 }
However, this seems like a bit more song-and-dance than should be necessary: qualifying references with their module name for every reference seems excessive. I would have assumed that I could do something like mod A::A
or use A::*
to 'import' types, functions, etc.
Is this the expected, idiomatic way to split code up?
Edit:
Per discussion, I updated my source to use use a::*
. New code is:
main.rs:
mod a;
mod b;
use a::*; // !
use b::*; // !
fn main() {
let a = A { a: 123 };
let b = B { b: 3.14 };
println!("{:?}", a);
println!("{:?}", b);
}
a.rs:
use crate::b::*;
#[derive(Debug)]
pub struct A {
pub a : u32
}
impl From<B> for A {
fn from(b : B) -> A {
A { a: b.b as u32 }
}
}
b.rs:
use crate::a::*;
#[derive(Debug)]
pub struct B {
pub b : f32
}
impl From<A> for B {
fn from(a : A) -> B {
B { b: a.a as f32 }
}
}
Including both mod a;
and use a::*;
(changed to follow snake_case
convention, but importantly to prevent name conflicts with the previously uppercase A
module from conflicting with the still-uppercase A
struct) now lets components be referenced without qualification.