I'm having an issue with lifetimes that I'm not sure how to solve, since it seems like the change I'm making should be trivial with regards to lifetimes.
Given:
use anyhow::Context;
use nom::{IResult, bytes::complete::tag};
The following code compiles:
let input = std::str::from_utf8(&output.stdout).unwrap();
let mut lines = input.lines();
let branch_line = lines.next().context("no output from `git status`")?;
let branch: IResult<&str, &str> = tag("On branch ")(branch_line);
let (branch, _) = branch.expect("failed to get name of current branch");
After changing the expect
in the final line to context
, the code no longer compiles:
let input = std::str::from_utf8(&output.stdout).unwrap();
let mut lines = input.lines();
let branch_line = lines.next().context("no output from `git status`")?;
let branch: IResult<&str, &str> = tag("On branch ")(branch_line);
let (branch, _) = branch.context("failed to get name of current branch")?;
error[E0597]: `output.stdout` does not live long enough
--> src/status.rs:303:41
|
303 | let input = std::str::from_utf8(&output.stdout).unwrap();
| ^^^^^^^^^^^^^^ borrowed value does not live long enough
...
307 | let branch: IResult<&str, &str> = tag("On branch ")(branch_line);
| ------------------- type annotation requires that `output.stdout` is borrowed for `'static`
...
436 | }
| - `output.stdout` dropped here while still borrowed
Looking at the docs for anyhow
, it doesn't appear to me like it should introduce any lifetime bounds on the &output.stdout
.
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
Scratching my head. Still new to lifetimes.
cannot find value 'output' in this scope
. Apart of that, you can't directly execute code in Rust, you need to write it in amain()
function. I know this seems nitpicky, but it's a way of respecting the SO community; the time that you saved by not writing a minimal reproducible example means that now I have to do it before I can start answering your question. – Chaperone