Hello!
I'm building a little tool to parse some output from a UNIX command
What I wanted was to capture the output, parse the lines to a struct and then do some computation
My current code:
let output = Command::new("ps")
.arg("aux")
.output()
.ok()
.expect("Failed to execute command");
let result =
String::from_utf8(output.stdout)
.map(split_lines)
.map(parse_lines_to_struct);
With split_lines
being:
fn split_lines<'a>(all_lines: &'a String) -> Vec<&'a str> {
all_lines
.lines()
.collect()
}
When running this, I get that the function needed on map should not have a lifetime param
error[E0631]: type mismatch in function arguments
--> proce.rs:17:10
|
17 | .map(split_lines)
| ^^^ expected signature of `fn(std::string::String) -> _`
...
21 | fn split_lines<'a>(all_lines: &'a String) -> Vec<&'a str> {
| --------------------------------------------------------- found signature of `for<'a> fn(&'a std::string::String) -> _`
I began to use the explicit lifetime param as my struct has a str field, which should not outlive the struct
struct Process<'a> {
user: &'a str,
}
What part am I doing wrong? Should the struct not have a lifetime param or is there a way to use map
or and_then
with lifetime params?
Thank so much for this space