Thus, this is a common mistake in rust, considering that the parameters of the lifetime affect the lifetime.
They donβt do this, they only let the compiler know that the link returned by the function takes some time. In any case, that would be true, but now the compiler is also aware of this and can provide more secure code. This is due to the fact that Rust conducts local analysis (while looking at one function).
In this case, you create a stack close. As the name implies, a stack closure is created on the stack and you then push it back onto the stack. This is similar to this C code:
int *foo() { int a = 5; return &a; }
Obviously, a pointer (or "link") to a invalid the moment you return. This is what prevents rust.
In this case, the lifetime of the stack closure continues for the duration of the function, but the lifetime parameter requires it to last longer than this (although there is nothing to say how long it is for sure), therefore, a mismatch error.
The basic rule of thumb is that if you have lifetime parameters for a function, you need each parameter at a position in the argument list and return type, otherwise you probably have something wrong.
If you really want to return a closure, then you should use the heap closure, ~fn (&str) -> ~[(T, int)] , since it is allocated on the heap and can be transferred more freely (although still not copied).
Aatch
source share