[package]
name = "rocketing_around"
version = "0.1.0"
[dependencies]
rocket = "0.4.0"
[dependencies.rocket_contrib]
version = "0.4.0"
default_features = false
features = [ "handlebars_templates" ]
compiler output:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'r` due to conflicting requirements
--> src/main.rs:19:5
|
19 | DynamicResponder::from(Template::render("template", ()))
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 18:10...
--> src/main.rs:18:10
|
18 | fn route<'a>() -> DynamicResponder<'a> {
| ^^
= note: ...so that the expression is assignable:
expected DynamicResponder<'a>
found DynamicResponder<'_>
= note: but, the lifetime must be valid for the static lifetime...
= note: ...so that the types are compatible:
expected rocket::response::Responder<'_>
found rocket::response::Responder<'static>
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: Could not compile `rocketing_around`.
Compiling tmp v0.1.0 (/home/matias/Documents/eclipse_workspace/tmp/tmp)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'r` due to conflicting requirements
--> src/main.rs:17:5
|
17 | DynamicResponder::from(Template::render("template", ()))
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 16:10...
--> src/main.rs:16:10
|
16 | fn route<'a>() -> DynamicResponder<'a> {
| ^^
= note: ...so that the expression is assignable:
expected DynamicResponder<'a>
found DynamicResponder<'_>
= note: but, the lifetime must be valid for the static lifetime...
= note: ...so that the types are compatible:
expected rocket::response::responder::Responder<'_>
found rocket::response::responder::Responder<'static>
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: Could not compile `tmp`.
To learn more, run the command again with --verbose.
When I run rustc --explain:
rustc --explain E0495
error: no extended information for E0495
I edited the code to make it work,changing a few things in the process:
I am a newbie when it comes to Rust; can you clarify a few things?
Why does the DynamicResponder have to contain a closure? Why can’t it call methods from the wrapped Responder?
Why does the root method need to return an object with static lifetime?
I was under the impression that static lifetime means that it exists from when the program starts executing until it terminates. How can an object that was just created in the root method have a static lifetime?
A closure is the way I chose to implement it because it was the easiest way to do it.
The chain of reasons it can’t be a wrapped Responder<_> are:
Responder::respond_to takes a self parameter,calling the method moves self into the method(by value).
methods that take self require self to be Sized
dyn Trait types are not Sized.
Box<_> does not implement Responder<_>.
I could not figure out any other lifetime that would work.
DynamicResponder<'static> means that the DynamicResponder doesn’t contain any references to things that live for less than the remaining program execution,you can drop a DynamicResponder<'static> whenever you want.
For the record, in rustc there are elevennow twelve different categories of lifetime-inference failure that result in E0495. (You can find them here). The variety of causes and the extensive information in the error sequences themselves are probably the reasons why rustc --explain E0495 does not result in any extended information.
Edit: Updated number of categories and location of relevant source within file.