Comment

Comment by unlinked_dll

You can make this a lot faster assuming the schema is fixed, and clean it up a bit with iterators. IE something like this

    #[derive(Copy,Clone,Deserialize)]
    struct Point {
        x: f64, 
        y: f64, 
        z: f64
    }

    #[derive(Copy,Clone,Deserialize)]
    struct Schema {
        coordinates: Vector<Point>
    }

    fn main() -> Result<(), Box<dyn Error>> {
        let mut json_content = String::new(); 

        File::open("1.json")? // don't need Path 
            .read_to_string(&mut json_content)?;

        let schema = serde_json::from_str::<Schema>(&json_content)?;
 
        let len = schema.coordinates.length(); 
        let (x, y, z) = 
            schema.coordinates
                  .iter()
                  .fold((0.0, 0.0, 0.0), 
                    |(xsum, ysum, zsum), (x, y, z)|{
                        (xsym + x, ysum + y, zsum + z)
                    }); 
        println!("x: {}, y: {}, z: {}", x / len, y / len, z / len);
      Ok(())
    }

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton