实例-rust-打开计算器

https://rust.ffactory.org/std/process/struct.Command.html

进程生成器,提供对如何生成新进程的细粒度控制。

可以使用 Command::new(program) 生成默认配置,其中 program 提供了要执行的程序的路径。

其他生成器方法允许在生成之前更改配置 (例如,通过添加参数) :

main.rs

use std::process::Command;

fn main() {

    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
                .arg("/C")
                .arg("calc")
                .output()
                .expect("failed to execute process")
    } else {
        Command::new("sh")
                .arg("-c")
                .arg("echo hello")
                .output()
                .expect("failed to execute process")
    };
    
    let hello = output.stdout;
    println!("{:?}", hello)
}



posted @ 2022-09-18 20:14  Nazorine  阅读(297)  评论(0)    收藏  举报