0

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;

  • 1
    You only need a `lib.rs` if you want to write other programs that use this library. If all you want is to split the code for a single program into different files, then all you need to do is add the corresponding `mod foo` lines to the top of your `main.rs`. – Jmb Nov 29 '22 at 07:44
  • Thanks,that's good to know. Begs the questions; however, if i decided to write my simulator as a set a libraries (I would put each related module in the lib.rs i presume) so that I could use it later. One such module is the Object module that can have characteristics of a rigid body. After moving, it to the lib.rs, I could then just use it by adding it to my dependencies in the .TOML file? – thephysicsprogrammer Nov 29 '22 at 15:04
  • See https://stackoverflow.com/questions/26946646/package-with-both-a-library-and-a-binary – Jmb Nov 29 '22 at 15:32
  • Does this answer your question? [Package with both a library and a binary?](https://stackoverflow.com/questions/26946646/package-with-both-a-library-and-a-binary) – cafce25 Nov 29 '22 at 16:01
  • @jmb @ cafce25 thx! From the book, it was saying to include a minimum executable with your lib.rs so I've been trying to implement this in practice – thephysicsprogrammer Dec 01 '22 at 16:20

0 Answers0