I want to iterate over array and find the best element with some corresponding values and then perform some calculation, now I perform the calculation on every element and return the best calculation which is unnecessary. The additional values here is closest_intersection_info. The compare method of IntersectionInfo :
pub fn compare(&mut self, other : Self) -> bool {
if (self.distance > other.distance) {
*self = other;
return true;
}
false
}
I just want to get closest_intersection_info and the corresponding element of the array without using unsafe or clone, in c++ I would use pointers, what experienced rustinace would use in this case
fn reytrace(&mut self, ray : Ray, scene : &Scene) -> Color {
let mut closest_intersection_info = IntersectionInfo::new();
let mut result_color = Color::new(0.0,0.0,0.0);
for element in &scene.elements {
if let Some(info) = element.geometry().intersect(&ray) {
if closest_intersection_info.compare(info) {
result_color = element.material().color(&ray, &closest_intersection_info, &scene.lights);
}
}
}
return result_color;
}