Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous lifetime in function return type

Tags:

rust

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.

like image 867
Rish Avatar asked Mar 12 '26 07:03

Rish


1 Answers

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.

like image 89
aedm Avatar answered Mar 15 '26 00:03

aedm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!