error[E0106]: missing lifetime specifier
--> src/main.rs:7:29
|
7 | pub fn from(store: u64) -> Difficulty {
| ^^^^^^^^^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
But the problem is the structure have 2 elements, one is lifetime parameter but another is not. How can I fix the build error but allow a element without lifetime?
It doesn't matter whether some elements of the structure doesn't have lifetime (as long as this is valid definition, of course). If any of them has it (non-static), the whole structure would also have it, and you've defined it as such: struct Difficulty<'a> { ... }. Now, anywhere it is used, the lifetime must be either elided or specified explicitly.
The function that doesn't receive any reference parameters can't be subject to lifetime elision (compiler has no way of telling how long the return value is allowed to live). It seems that in this case you won't be using any &str value at all, so it's possible to mark output parameter as Difficulty<'static>.
I can't make it as 'static, these code is used to demo this lifetime build problem, the real situation is much more complex and both from() function will be called frequently.
So, perhaps the only way to fix this is to split into 2 structures
Well, so it means that you could do let diff = Difficulty::from(number) and then set diff.batch in some other way, right? Then you must guarantee that your Difficulty struct won't live too long. Since Difficulty::from has no ways of figuring it by itself, it seems that the function itself must be generic:
These are two independent parameters - one for the whole struct, another for the function (note that function doesn't receive any argument of Difficulty type, so it doesn't have to depend on its lifetime). When we declare type in fn from<'b> as Difficulty<'b>, it means "take any lifetime parameter passed to this function (let it be 'static by default) and pass it to the Difficulty as its parameter, however it does call it".