Hello!
I have some simple code below:
fn main() {
// Construct type for particle
struct Particle<T,U,V>{
pos: (T, T, T),
vel: (U, U, U),
rhop: V
}
// Implement print command for particle
impl<T: std::fmt::Display,U: std::fmt::Display,V: std::fmt::Display> std::fmt::Display for Particle<T,U,V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Position: ({}, {}, {})\nVelocity: ({}, {}, {})\nDensity: {}", self.pos.0, self.pos.1,self.pos.2, self.vel.0,self.vel.1,self.vel.2,self.rhop)
}
}
// Define initial particle
let position = (5.0,0.0,0.0);
let velocity = (0.0,0.0,0.0);
let density = 1000.0;
// Set parameters of the particle
let p = Particle {pos: position, vel: velocity ,rhop: density};
// Print the particle
println!("{}",p);
}
I have tried to write it in a way so that it is as generic as possible and can accept different types for different properties. In reality only f32 or f64 would probably be used. I just posted this bit here, curious if anyone would write this differently, and what the advantages would be compared to this.
Kind regards