The intention is to take the output of calling a binary, get the last line of that output, split it, and get the string at index 1.
Here is the attempt:
let result:Result<String, FromUtf8Error> = String::from_utf8(output.stdout);
let value = result.map(move |res| {
res.lines()
.last()
.unwrap_or(String::from("").as_ref())
.split(" ")
.collect::<Vec<&str>>()[1]
});
Two issues:
First one, the above code leads to compilation error:
57 | / res.lines()
58 | | .last()
59 | | .unwrap_or(String::from("").as_ref())
| | ---------------- temporary value created here
60 | | .split(" ")
61 | | .collect::<Vec<&str>>()[1]
| |______________________________________^ returns a value referencing data owned by the current function
second one, I realise the type of value
is Result<<unknown>,FromUtf8Error>
instead of it being Result<<String>,FromUtf8Error>
as I expected.
So how to fix the compilation error and get the type right?
You Are trying to return a &str
that is pointing to data owned by the map
closure. Same thing as
fn does_not_work() -> &str {
let my_string = String::new();
&my_string
}
You need to have an owned value, such as String
, to return with.
result.map(move |res| {
String::from(
res.lines()
.last()
.unwrap_or("")
.split(" ")
.collect::<Vec<&str>>()[1]
)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.