Skip to content

Instantly share code, notes, and snippets.

@hufeng
Created November 16, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hufeng/c28df46e0d0e3f5110811861064d22d8 to your computer and use it in GitHub Desktop.
Save hufeng/c28df46e0d0e3f5110811861064d22d8 to your computer and use it in GitHub Desktop.
rust - command -demo
use std::env::args;
use std::process::Command;
fn main() {
// get command args
let arg: Vec<String> = args().collect();
// get compile filename
let filename = &arg[1];
println!("{}", filename);
// compile
let output = Command::new("rustc")
.arg(filename)
.output()
.expect("rustc compile error");
println!("{}", String::from_utf8_lossy(&output.stdout));
// execute
let strs: Vec<&str> = filename.split("/").collect();
let execute = (strs[strs.len() - 1]).trim_end_matches(".rs");
println!("{}", execute);
let sh = vec![".", execute].join("/");
let output = Command::new(sh)
.output()
.expect("failed to execute command");
println!("{}", String::from_utf8_lossy(&output.stdout));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment