Problem with chart lifetime

I am using Plotters library (GitHub - plotters-rs/plotters: A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely πŸ¦€ πŸ“ˆπŸš€) to draw chart in web browser using rust compiled to webassembly. Recently I ran into a problem when I was trying to pass the chart to websocket set_on_message event and doing something with the graph there. This is the code where I encountered a problem:

let canvas_backend = CanvasBackend::new("canvas").expect("cannot find canvas");
let root_drawing_area = canvas_backend.into_drawing_area();

let mut chart = ChartBuilder::on(&root_drawing_area)
  .set_label_area_size(LabelAreaPosition::Left, 50)
  .set_label_area_size(LabelAreaPosition::Bottom, 50)
  .build_cartesian_2d((1..1000).log_scale(), (1..1000).log_scale())

let mut client = wasm_sockets::EventClient::new("ws://localhost:8000/ws")?;

  move |client: &wasm_sockets::EventClient, message: wasm_sockets::Message| {
    let range = chart.x_range();

Compiler is telling me an error: error[E0597]: root_drawing_area DOES NOT LIVE LONG ENOUGH.

I guess I need to change root_drawing_area ownership to the closure, which i am passing to the set_on_message event. But I still cannot figure out how.
I have also tried to make the variables static, but also without success.

Could someone help me please with this problem? Thank you.

I have never used Rust with WASM before, so it’s hard for me to compile your example without first looking up many details on how that’s done that are completely unrelated to the question. Please provide the entire compiler error. If you have a hard time seeing the whole error in an IDE, try using the output from building through the command line instead.

In short, you just cannot do this.

The closure passed to EventClient::set_on_message has an implicit 'static lifetime bound. Anything that this closure has access to cannot contain temporary references. chart contains a temporary reference to the CanvasBackend. It's temporary because the backend will be dropped at the end of main().

Probably what you want is the tried and true Communicating Sequential Processes architecture. Which means you pass the write-end of a channel to the closure, and it sends relevant information from the event back to the main thread through the channel. The main thread can use the information it receives to interact with the chart.