Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

rust - How do I create a Path with a static lifetime from a string with a short lifetime?

I'm currently trying to implement a shell built in Rust for practice, I've managed to implement most features, however, I'm currently trying to make a built-in command which takes path <arg1> <arg2> ..., and adds this to my program's path variable. However, I have this setup in that I have a loop which parses the command input and produces a command variable as a &str and its arguments as Vec<&str> which form my Command struct.

For this built-in command, path, I need to take the arguments and create a new vector of paths and replace the existing path. My current path variable is declared outside of my loop and is a Vec<&Path>. I'm currently receiving an error:

lifetime mismatch but data from `command` flows into `path` here

Which is understandable, but I've tried setting my args to have a 'static lifetime, but this failed as the borrowed value does not live long enough, as I read the line within the loop and replace it and process the string into my command struct. Would appreciate any help!

let line = stdin.lock().lines().next().unwrap().unwrap();
let split = line
       .split_whitespace()
       .enumerate()
       .filter(|&(i, _)| i > 0)
       .map(|(_, e)| e);
let args = split.collect::<Vec<&str>>();
// Command Struct
struct Program<'a> {
    command: &'a str,
    args: Vec<&'static str>,
}
// Dispatcher
fn dispatch_command<'a>(command: Program, path: &mut Vec<&Path>) -> Result<(), &'a str>
question from:https://stackoverflow.com/questions/65832607/how-do-i-create-a-path-with-a-static-lifetime-from-a-string-with-a-short-lifetim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...