Created
November 16, 2020 12:33
-
-
Save hufeng/c28df46e0d0e3f5110811861064d22d8 to your computer and use it in GitHub Desktop.
rust - command -demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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