Rust常见库使用示例。
标准库
命令行参数
$ cargo run abcdef
Compiling test123 v0.1.0 (/Users/xiexianbin/workspace/code/github.com/xiexianbin/rust-study/test123)
Finished dev [unoptimized + debuginfo] target(s) in 0.62s
Running `target/debug/test123 abcdef`
["target/debug/test123", "abcdef"], args[0]: "target/debug/test123"
读文件
use std::fs;
fn main() {
let c = fs::read_to_string("./main.rs").expect("file main.rs not found.");
println!("{:?}", c);
}
读环境变量
use std::env;
let name = env::var("ENV_NAME").is_err();
sleep
use std::thread;
use std::time::Duration;
// sleep 1 second
thread::sleep(Duration::from_secs(1));