简体   繁体   English

Rust 克隆/复制问题

[英]Rust clone/ copy issues

trying to wrap my head around how to do some simple plotting in egui.试图围绕如何在 egui 中进行一些简单的绘图。 I have a data member in myapp struct that is a Vec::<Value>.我在 myapp 结构中有一个数据成员,它是 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.有没有办法将它传递到 Points::new(Values::from_values(data.to_vec()) 而不创建值的副本?示例通常是动态生成的,但是从光盘和解析每一帧的文本数据。

   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.解析一次并将结果存储在持久结构中,并在show结束期间从那里复制。

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.也就是说,看起来您确实需要每帧创建一个新的Points object,因为plot_ui.points按值获取点 object。 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.你现在做的方式——存储一个Vec<Value>并在每一帧克隆它——可能是你会得到的最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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