0

I am trying to build a wrapper struct around the gnuplot lib for Rust.

I want to plot multiple sets of lines in the same 2D-axes object, therefore I need to keep the Axes2D around.

How can I fix this structure? The figure and the axes fields will live exactly as long as the plot struct itself.

use gnuplot::{Axes2D, Figure, Caption, Color};

pub struct Plot {
    figure: Figure,
    axes: &mut Axes2D,
}

impl Plot {
    pub fn new() -> Plot {
        let fig = Figure::new();
        Plot {
            figure: fig,
            axes: fig.axes2d(),
        }
    }
}

This fails with the following compiler error:

plot.rs:6:11: 6:22 error: missing lifetime specifier [E0106]
plot.rs:6     axes: &mut Axes2D,

I've tried adding lifetime 'a at the struct, field and impl, but this gives me different errors. Since I'm not sure what I'm doing, I was wondering if someone could explain how to achieve this.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jesse
  • 315
  • 2
  • 8
  • 2
    I think that http://stackoverflow.com/questions/32300132/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct will answer your question. – Shepmaster Aug 31 '15 at 15:08
  • I'll have a look, thanks. I did try google before I posted this btw. :) – Jesse Aug 31 '15 at 15:09
  • No worries, it's a common problem and why that question was created. Hopefully, we will be able to mark a bunch of things as duplicates to make the SEO way better. :-) – Shepmaster Aug 31 '15 at 15:10
  • Do you suggest using the owing_ref here, or is there a more idiomatic way to handle this? – Jesse Aug 31 '15 at 15:38
  • It looks like having multiple structs is more appropriate. – Shepmaster Aug 31 '15 at 15:41
  • Putting the Axes2D in its own struct? Could you provide an example? – Jesse Aug 31 '15 at 15:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88388/discussion-between-jesse-and-shepmaster). – Jesse Aug 31 '15 at 15:49

0 Answers0