fn execute_os_command(command: &str) -> String {
let cmd_output = Command::new("sh")
.arg("-c")
.arg(command)// command
.output()
.expect("Failed to Execute OS Command");
let result = String::from_utf8(cmd_output.stdout).unwrap();
result
}
@chrefr Thanks for your answer. I thought about that, and the only reason that I wanted to return a reference instead of the whole value(copy) is that the string that I am returning could be really really long. any advice on that?
If you're sure the text is valid UTF-8, and you want to save the check, you can fall to using the unsafe String::from_utf8_unchecked(). Don't do that unless profiling reveals performance problems, however.
Also, going by the topic name (Cannot return value referencing local data), you were trying to do something that is fundamentally unsound. Values defined on the stack will cease to exist when the function returns and the stack frame is dropped. So, you can never return a reference to a local variable, safely.