Hello, so I am quite new to Rust, and I just run into this issue relating to a value may not live long enough. I tried to search for similar questions, but none of them is similar "enough" for me to know what should be done.
I am trying to write a simple program that draw graphs, using plotter
.
use plotters::prelude::*;
pub fn gen_ven<T: plotters::drawing::IntoDrawingArea>(backend: T) -> Result<(), Box<dyn std::error::Error>> {
let root = backend.into_drawing_area();
root.fill(&WHITE)?;
root.present().unwrap();
Ok(())
}
Look simple enough, but upon compiling, I got
error[E0310]: the associated type `<T as plotters::prelude::DrawingBackend>::ErrorType` may not live long enough
--> src\ven.rs:6:5
|
6 | root.fill(&WHITE)?;
| ^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as plotters::prelude::DrawingBackend>::ErrorType: 'static`...
= note: ...so that the type `<T as plotters::prelude::DrawingBackend>::ErrorType` will meet its required lifetime bounds
So I presume this error occurs because the error inferred from the fill trait does not "live long enough" or something, as when I do un_wrap
, i.e root.fill(&WHITE).unwrap();
, then everthing worked fine.
However, for this random function, the code compiled:
fn find_char_index_in_first_word(text: &str, char: &char) -> Result<usize, Box<dyn std::error::Error>> {
let first_word = match text.split(" ").next().is_some() == true {
true => text.split(" ").next().ok_or("error happen")?,
false => return Ok(0)
};
let res = first_word.find(|x| &x == char);
match res {
Some(x) => Ok(x),
None => Ok(0),
}
}
The ?
worked, which I presume it's what the ?
should be doing.
So what happened in my first case?
Thank you.