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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.