rust cmd stdio demo
Posted u010278923
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rust cmd stdio demo相关的知识,希望对你有一定的参考价值。
use std::process::{Command, Stdio};
use std::io::{BufRead, BufReader, Write, stdin, Read};
use std::fmt::Error;
use std::thread::{spawn};
fn main() {
run();
}
fn run() -> Result<(),Error> {
let result = Command::new("sh")
//.arg("google.com")
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.spawn()
.unwrap();
let stdout = result.stdout.unwrap();
let reader = BufReader::new(stdout);
let mut cstdin = result.stdin.unwrap();
let input = spawn(move||{
loop{
let mut cmd = String::new();
stdin().read_line(&mut cmd);
cstdin.write(&cmd.as_bytes()).expect("err write");
}
});
reader.lines()
.filter_map(|line| line.ok())
.for_each(|line| println!("{}", line));
input.join();
Ok(())
}
以上是关于rust cmd stdio demo的主要内容,如果未能解决你的问题,请参考以下文章