Skip to content
เข้าสู่ระบบ

Process Functions

Process และ command execution functions

use std::process::Command;
fn main() {
// Basic command execution
let output = Command::new("echo")
.arg("Hello")
.output()
.expect("Failed to execute");
println!("Output: {}", String::from_utf8_lossy(&output.stdout));
// With multiple args
let output = Command::new("ls")
.args(["-l", "-a", "-h"])
.output()
.expect("Failed");
println!("ls output lines: {}",
String::from_utf8_lossy(&output.stdout).lines().count());
// Shell command
let output = Command::new("sh")
.arg("-c")
.arg("echo $HOME && pwd")
.output()
.expect("Failed");
println!("\nShell output:\n{}", String::from_utf8_lossy(&output.stdout));
// status() - just exit code
let status = Command::new("true")
.status()
.expect("Failed");
println!("true exit code: {:?}", status.code());
// spawn() - background process
let child = Command::new("sleep")
.arg("0.1")
.spawn()
.expect("Failed");
println!("Child PID: {}", child.id());
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด