I've read the Rust Book and some other tutorials on lifetimes. I thought I was understanding lifetimes as like a generic parameter that a function takes, and that how you assign it to the input and output parameters of your function constrains the lifetime of those parameters. So like fn find<'a, 'b>(pattern: &'a str, text: &'b str) -> &'b str means anything passed in as text needs to have a lifetime at least as long as the return value. But on a program I'm writing I got an error that makes me think I'm misunderstanding something fundamental about lifetimes.
I have a function with signature:
async fn check_git(requester: &mut Requester) -> Result<()>
Result is anyhow::Result if it matters.
When I compile I get the error:
error[E0726]: implicit elided lifetime not allowed here
--> src/scanner/git.rs:5:40
|
5 | pub async fn check_git(requester: &mut Requester) -> Result<()> {
| ^^^^^^^^^- help: indicate the anonymous lifetime: `<'_>`
|
= note: assuming a `'static` lifetime...
I fixed the compiler error by adding a lifetime parameter: async fn check_git<'a>(requester: &mut Requester<'a>) -> Result<()>.
I understand requester can't have a static lifetime, as the way I'm calling check_git, the lifetime of the requester I pass in is the scope of the function calling check_git. But I don't understand exactly the meaning of the lifetime annotations I added.
My beginner understanding of lifetimes was that fn check_git<'a> is just specifying that 'a is a generic parameter, and then when you apply 'a to more than one parameter it constrains how the lifetimes of those parameters relate. But when I only apply 'a to one parameter there's no added constraint or meaning from the lifetime annotation?
The Requester struct I'm adding the lifetime parameter to is defined as:
struct Requester<'a> {
client: SendRequest<Body>,
resp: Option<Response<Body>>,
host: &'a str,
port: u16,
ctx: &'a SslContextRef,
}