Looking at refactoring some graphical code I wrote by splitting it into separate modules to further advance my RUST abilities.
The program is a simple 2d representation of linear conservation for non-rotating, for now, rigid bodies. Thus far, I have implemented everything into a single file while I'm learning rust, but I'm now moving on to understanding the file structures and wish to separate the code.
My questions is what do I use the lib.rs file for when I already have a main.rs? Extra reading has suggested keeping the main to a minimum and run from the library (i.e. include a run method in the lib.rs that you call from main.rs). How do I do this by the way?
I currently use a src/main.rs, src/simulation.rs, src/simulation/objects/mod.rs, src/closed_body/mod.rs, src/simulation/functions/mod.rs
I thought about putting the graphical control in the library. Any suggestion or feedback relating to the layout of such a program is greatly appreciated. Especially any specific use ...mods i need to include
src/main.rs
/*
The compiler will look for the module’s code in these places:
Inline, within curly brackets that replace the semicolon following mod garden
In the file src/garden.rs
In the file src/garden/mod.rs
*/
pub mod simulation;
fn main() {
let mut demo = simulation.generate().build();
demo.run();
}
src/lib.rs
// utilize lib.rs to handle the canvas / drawing / loop update/ Graphics will bu using SDL2 in 2D
// is this the proper method?
use crate::simulation::object;
use crate::simulation::closed_system;
use crate::simulation::functions;
pub fn run() {
}
src/simulation.rs
pub mod object;
pub mod closed_system;
src/simulation/object/mod.rs
// src/simulation/object/mod.rs
#[derive(Debug, Copy, Clone)]
pub enum Vector {
Position ((f64, f64)),
Velocity ((f64, f64)),
Acceleration((f64, f64)),
Momentum ((f64, f64)),
}
use crate::simulation::object::Vector as Position;
use crate::simulation::object::Vector as Velocity;
use crate::simulation::object::Vector as Acceleration;
use crate::simulation::object::Vector as Momentum;
impl Vector {
}
#[derive(Debug, Copy,Clone)]
pub struct Object {
mass: f64,
position: Position,
velocity: Velocity,
acceleration: Acceleration,
momentum: Momentum,
}
impl Object {
// -- method --
// -- associated functions
pub fn new(mass: f64, position: Position, velocity: Velocity, acceleration: Acceleration, momentum: Momentum) -> Self {
Self {
mass,
position,
velocity,
acceleration,
momentum,
}
}
}
src/simulation/closed_system/mod.rs
// consider data structure, closed_system, to hold 1-20 objects (free, rigid bodies)
src/simulation/functions/mod.rs
pub mod object;
pub mod closed_system;