As for x
versus ref x
, x
does not work because you only have a reference to self
and therefore cannot derive the value E1
from it - all you can do is take a link to it.
But now the most important thing: you incorrectly defined your description
method, and the Rust compiler does not warn you about this, but rather makes life unpleasant for you.
This is the actual definition of the description
method:
fn description(&self) -> &str;
Note carefully: &str
, not &'static str
. The compiler should have objected to 'static
in the signature, but, alas, this did not happen. (This is the topic https://github.com/rust-lang/rust/issues/21508 , filed with this issue in mind.) Typically, specifying a longer lifetime will be just fine, it will simply reduce the size to size, but in some situations it won't do what you thought it was on purpose, he changed the E1
s description
method to return &str
with its own lifetime, but in E2
, he still wants to return &'static str
. Of course, the x
reference is not 'static
, so it cannot do this. Confused, huh? Do not worry, basically it's not your fault!
To fix this, remove all occurrences of 'static
to match the definition of the attribute. Then, since x
is inside self
, the lifetime will correspond accordingly.
source share