error[E0597]: `req` does not live long enough
--> src/main.rs:36:20
|
36 | http_path: req.uri().path(),
| ^^^------
| |
| borrowed value does not live long enough
| argument requires that `req` is borrowed for `'static`
...
52 | }
| - `req` dropped here while still borrowed
error[E0597]: `req` does not live long enough
--> src/main.rs:37:22
|
37 | http_method: req.method().as_str(),
| ^^^---------
| |
| borrowed value does not live long enough
| argument requires that `req` is borrowed for `'static`
...
52 | }
| - `req` dropped here while still borrowed
What is the right way of handling http path and methods?
Thanks @Fiedzia, I am not sure what you mean. For string like "POST" which can stay in memory the entire lifetime of the program, what is the best way in Rust to handle? In Ruby I would use symbols (:something) so that it gets allocated once in memory and I can keep referring to it from all over the place and never mutate it.
For string like "POST" which can stay in memory the entire lifetime of the program, what is the best way in Rust to handle?
for strings it would be:
static POST:&str = "POST";
but you don't need a string here, enum would be better.
so what you could have:
struct HReq<'a> {
http_path: &'a str,
http_method: &'a str,
}
or with enums:
enum HttpMethod {
POST,
GET,
HEAD
//...(it is probably already defined by whatever library you are using)
}
struct HReq<'a> {
http_path: &'a str,
http_method: HttpMethod,
}
Practically, &'static str means string literal which is embeded directly on the data section of the program executable. If you want dynamically constructed owned string, use String instead which is a growable buffer contains UTF-8 data.