Skip to content

Dealing with env - lifetime is getting difficult #26637

Closed
@emabee

Description

@emabee

Here's a mini version of code I destilled from my attempts to deal with env::args.

// Why does this fail, and how can I fix it?
use std::env;
fn main() {
    let mut vec0: Vec<&str> = vec![];
    let vec1: Vec<_> = env::args().collect();
    let s = vec1.get(0).unwrap();              // `vec1` does not live long enough
    vec0.push(&s);
}

// Note: this does compile:
// fn main() {
//     let mut vec0: Vec<&str> = vec![];
//     let vec1: Vec<_> = vec!["foo", "bar"];;
//     let s = vec1.get(0).unwrap();
//     vec0.push(&s);
// }

I'd appreciate some help and explanation - I've tried a lot already, using to_owned() etc.
What I wanted to do: learn rust by writing code. Here I wanted to deal with program parameters (intentionally ignoring the libs crates.io).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions