3 ms·
So, every reference has a lifetime. But writing them out all the time isn't great for humans. So we looked at existing code, and found that the vast majority of
by steveklabnik 6y ago
So, every reference has a lifetime. But writing them out all the time isn't great for humans. So we looked at existing code, and found that the vast majority of lifetimes followed a few simple patterns. So we implemented these patterns as "elision rules," that is, patterns where you can not write, that is, elide, the lifetime.
Here are the rules:
1. Every reference as a parameter gets its own lifetime parameter. There is only one in this case.
2. If there is exactly one, then every possible lifetime in the return type is assigned to it.
3. If there are multiple parameters, but it's a method, not a free function, then the lifetime assigned to self is assigned to all outputs.
This is written in a bit of an abstract way, but, #2 there describes this function: we have one input, so we assign the same lifetime to all outputs, and there's only one. This means that it's not 'static, and in fact, it would fail to compile if it assumed that.
The original RFC, if you're curious https://rust-lang.github.io/rfcs/0141-lifetime-elision.html https://rust-lang.github.io/rfcs/0141-lifetime-elision.html
(And, I actually think there's been some tweaks over time; if you have no inputs, the outputs can turn into 'static... the core of it is these three things though. Oh yeah, that is in the reference, which is the up to date spec, https://doc.rust-lang.org/reference/lifetime-elision.html; https://doc.rust-lang.org/reference/lifetime-elision.html; I linked the RFC for historical reference because it's interesting, and has some neat stats.)
- oconnor663 6y ago(For some reason the semicolon at the end of your reference link seems to mess it up. Here's the working link: https://doc.rust-lang.org/reference/lifetime-elision.html https://doc.rust-lang.org/reference/lifetime-elision.html )
- 205guy 6y agoMaybe because a semicolon is valid in URLs. Although the top answer on stack overflow also says: "However, if you're plucking them from relatively unstructured prose, it's probably safe to assume a semicolon at the end of a URL is meant as sentence punctuation." Because you can never know how a given website will handle the input, I always leave a space after URL and before punctuation in a text box, like so: http://example.com http://example.com ;
- deleted 6y ago[deleted]