Search code examples
rustegui

Rust clone/ copy issues


trying to wrap my head around how to do some simple plotting in egui. I have a data member in myapp struct that is a Vec::<Value>.

Is there a way to pass that into Points::new(Values::from_values(data.to_vec()) without creating a copy of the values? Examples generally generate on the fly but it feels a bit excessive to reading in from disc and parse the text data for each frame.

   struct MyApp {
   data: Vec<Value>,
   }
   myplot.show(ui, |plot_ui| {
                   let points = Points::new(Values::from_values(data.to_vec())); 
                   plot_ui.points(points);

Solution

  • but it feels a bit excessive to reading in from disc and parse the text data for each frame.

    You don't have to (and should not) do this every frame. Parse it once and store the results in a persistent structure, and copy from there during your show closure.

    That said, it does look like you will need to create a new Points object every frame, as plot_ui.points takes the points object by value. The way you are doing it now - storing a Vec<Value> and cloning it each frame - is probably the best you are going to get.