I'm having some trouble trying to grasp why I can't return an &str
value generated from a String
(goodness, when will as_str
be ready?) and I'm doing something wrong. I get this idea because nothing that I do makes the value live long enough to use.
I'm trying to implement error::Error
for a custom struct:
impl error::Error for LexicalError {
fn description(&self) -> &str {
let s = format!("{}", self);
// s doesn't live long enough to do this, I've tried
// cloning s and using that, but still the clone doesn't
// live long enough.
s.trim()
}
fn cause(&self) -> Option<&error::Error> {
None
}
}
(for the complete snippet, here is the playpen)
I can't figure out how to return an &str from description
, I'd like to reuse the Display
logic, unless of course I'm completely misunderstanding what description
should be returning (perhaps the short description of the issue). Either, I get the same issue with the return of format!(...)
which is a variable I can't seem to get to live long enough to be useful to me.
description
is supposed to be a description of the error not going into details;fmt::Display
is supposed to be there to augment it with details as desired. – Sapro