4 ms·
That function really is a good example for explaining lifetimes in Rust.
by db48x 6y ago
That function really is a good example for explaining lifetimes in Rust.
- rob74 6y agoThe function may be a good example, but this blog post doesn't really do a good job at explaining it, at least not to people unfamiliar with Rust (which I assume it's aimed at). If I understood correctly, if you assign the lifetime 'a (or 'x, 'l, whatever) to all parameters, you tell the compiler that they all have the same lifetime? And if you wanted to have different lifetimes, you could use 'a and 'b? But the article doesn't explicitly mention that, so you could end up thinking that 'a is some kind of a magical constant that you have to use...
- brundolf 6y agoThe target audience was programmers who are at least familiar with the basic concepts of stack, heap, and pointers, including but not limited to newer Rust programmers. You make a good point about 'a; I could have made that clearer. In practice it works the same way as a generic type (the letter is arbitrary, and it's declared inside the < >), which I assumed people would be familiar with, but I can see how the connection between the two may not have been obvious.
- Sharlin 6y agoYep, you’re correct. Naming the first (especially when it’s also the only) lifetime parameter ’a is just a convention, like naming the first type parameter T.
- ragnese 6y agoThat's a good point. When I first started trying to learn Rust, I also thought that there was some semantic meaning to 'a, since everyone uses that exact same name in all the examples.