我正在嘗試在 Unix 環境中對 Rust 執行 shell 命令,所以我嘗試了以下操作:
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::os::unix::prelude::CommandExt;
use std::process::Command;
pub fn main() {
let mut command = Command::new("ls");
println!("Here is your result : {:?}", command.output().unwrap().stdout);
}
我獲得了一個 u8 串列,我想知道如何獲得我的檔案項串列?這是一種更簡單的方法嗎?謝謝 :)
uj5u.com熱心網友回復:
您可以stdout像這樣將其轉換為 String 物件:
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::os::unix::prelude::CommandExt;
use std::process::Command;
use std::str;
pub fn main() {
let mut command = Command::new("ls");
let output = command.output().unwrap();
println!("{}", str::from_utf8(&output.stdout[..]).unwrap());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451685.html
上一篇:如何系結功能魚殼插件
下一篇:替換數值
