How do I copy a string so I can return it from a function in rust?

  Kiến thức lập trình

I’m new to Rust, please bear with me.

I’m writing a HTTP server using axum, and am writing a handler. It calls some external program somecommand, writes some string to its stdin, and reads stdout. The stdout output is formatted like a HTTP request (i.e. Header: ValuernHeader2: ValuernrnBody).

I want to turn that stdout output into an axum response.

async fn handle_request() -> impl IntoResponse {
    let mut child = Command::new("somecommand")
    .stdin(Stdio::piped())
    .spawn()
    .expect("Failed to spawn");

    let mut stdin = child.stdin.take().expect("Failed to open stdin");
    stdin
        .write_all("some string".as_bytes())
        .expect("Could not write to stdin");

    let output = child
        .wait_with_output()
        .expect("Failed to wait with output");

    let stdout = String::from_utf8(output.stdout)
        .expect("failed to parse stdout as utf8")
        .to_string();
    let r = stdout.split("rnrn").collect::<Vec<&str>>();

    let mut headers = HeaderMap::new();

    let lines: Vec<&str> = r[0].lines().collect();
    lines.iter().for_each(|line| {
        let parts: Vec<&str> = line.splitn(2, ": ").collect();
        headers.insert(
            parts[0],
            parts[1].parse().expect("Could not parse header value"),
        );
    });

    (headers, r[1].to_string())
}

The error I’m getting is stdout does not live long enough, which, if I understand things correctly, is because headers.insert(parts[0] ..., because part[0] lives only in this function scope, but since I’m returning it (as part of the headers map) it must have a longer lifetime.

So how do I increase the lifetime of parts[0] (or stdout) so that I can return it?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT