'Anonymous lifetime in function return type
I was looking at the source code of Rust, and found this function.
pub fn tokenize(input: &str) -> impl Iterator<Item = Token> + '_ {
let mut cursor = Cursor::new(input);
std::iter::from_fn(move || {
if cursor.is_eof() {
None
} else {
cursor.reset_len_consumed();
Some(cursor.advance_token())
}
})
}
I understand that '_ refers to an anonymous lifetime, but I'm not sure what it means in this context. Would love to get some clarification on this. Thanks.
Solution 1:[1]
It's a shorthand for
pub fn tokenize<'a>(input: &'a str) -> impl Iterator<Item = Token> + 'a { ... }
Rust sometimes allows you to avoid declaring lifetimes when the declaration is unambiguous:
fn foo(input: &'a str) -> &'a str { ... }
// same as
fn foo(input: &str) -> &str { ... }
But in the code above, the return type is not a reference, so this shorthand can't be used. '_ is a syntactic sugar for this case.
But why can't the '_ be eluded, too? Here's the answer from RFC 2115: argument_lifetimes:
The '_ marker makes clear to the reader that borrowing is happening, which might not otherwise be clear.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chayim Friedman |
