Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't use with nom error #317

Closed
Dangerise opened this issue Aug 9, 2023 · 2 comments
Closed

Couldn't use with nom error #317

Dangerise opened this issue Aug 9, 2023 · 2 comments
Labels

Comments

@Dangerise
Copy link

Sorry about my poor English, tell me anything you don't understand

I try to use anyhow when I'm writing a with nom , then I set the return type as anyhow::Result

here is a simple example

fn ftag(input: &str) -> IResult<&str, &str> {
    use nom::bytes::complete::tag;
    tag("a")(input)
}

fn anyhow_with_nom(input: &str) -> anyhow::Result<&str> {
    let (input, _) = ftag(input)?;
    Ok(input)
}

The error message :

error[E0521]: borrowed data escapes outside of function
   --> src\lexer.rs:121:22
    |
120 | fn anyhow_with_nom(input: &str) -> anyhow::Result<&str> {
    |                    -----  - let's call the lifetime of this reference `'1`
    |                    |
    |                    `input` is a reference that is only valid in the function body
121 |     let (input, _) = ftag(input)?;
    |                      ^^^^^^^^^^^
    |                      |
    |                      `input` escapes the function body here
    |                      argument requires that `'1` must outlive `'static`


It will be fine if I turn anyohw::Result into nom::IResult

fn ftag(input: &str) -> IResult<&str, &str> {
    use nom::bytes::complete::tag;
    tag("a")(input)
}

fn anyhow_with_nom(input: &str) -> IResult<&str, &str> {
    let (input, parttern) = ftag(input)?;
    Ok((input, parttern))
}

@dtolnay
Copy link
Owner

dtolnay commented Aug 10, 2023

I don't expect that anyone will be available to help you with this in this issue tracker.

If this is still an issue, you could try taking this question to any of the resources shown in https://www.rust-lang.org/community.

@j16r
Copy link

j16r commented Oct 15, 2023

Good answer and solution here: https://stackoverflow.com/a/73506323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants