简体   繁体   中英

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM