从命令行读取参数
use std::env; fn main() { let args: Vec<String> = env::args().collect(); println!("{:?}", args); }
ai@aisty:/opt/wks/rust/rfil/rcmd/target/debug$ ./rcmd aa bb cc ["./rcmd", "aa", "bb", "cc"]
第一个参数是命令本身
The
args
Function and Invalid UnicodeNote that
std::env::args
will panic if any argument contains invalid Unicode. If your program needs to accept arguments containing invalid Unicode, usestd::env::args_os
instead. That function returns an iterator that producesOsString
values instead ofString
values. We’ve chosen to usestd::env::args
here for simplicity, becauseOsString
values differ per platform and are more complex to work with thanString
values.
索引为0的参数是命令本身,从索引为1的参数开始才是输入的参数
use std::env; fn main() { let args: Vec<String> = env::args().collect(); let query = &args[1]; let filename = &args[2]; println!("Searching for {}", query); println!("In file {}", filename); }
ai@aisty:/opt/wks/rust/rfil/rcmd$ cargo run name /tmp/aa.txt Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/rcmd name /tmp/aa.txt` Searching for name In file /tmp/aa.txt
读取指定的文件内容
use std::env; use std::fs; fn main() { let args: Vec<String> = env::args().collect(); let filename = &args[1]; println!("In file {}", filename); let contents = fs::read_to_string(filename) .expect("Something went wrong reading the file"); println!("With text: {}", contents); }
ai@aisty:/opt/wks/rust/rfil/rcmd$ cargo run /tmp/aa.txt Compiling rcmd v0.1.0 (/opt/wks/rust/rfil/rcmd) Finished dev [unoptimized + debuginfo] target(s) in 0.21s Running `target/debug/rcmd /tmp/aa.txt` In file /tmp/aa.txt With text: aa
use std::env; use std::fs; fn main() { let args: Vec<String> = env::args().collect(); let (query, filename) = parse_config(&args); // --snip-- println!("Searching for {}", query); println!("In file {}", filename); let contents = fs::read_to_string(filename) .expect("Something went wrong reading the file"); println!("With text: {}", contents); } fn parse_config(args: &[String]) -> (&str, &str) { let query = &args[1]; let filename = &args[2]; (query, filename) }
ai@aisty:/opt/wks/rust/rfil/rcmd$ cargo run aa /tmp/aa.log Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/rcmd aa /tmp/aa.log` Searching for aa In file /tmp/aa.log With text: aa bb
use std::env; use std::fs; fn main() { let args: Vec<String> = env::args().collect(); let config = parse_config(&args); println!("Searching for {}", config.query); println!("In file {}", config.filename); let contents = fs::read_to_string(config.filename) .expect("Something went wrong reading the file"); println!("With text: {}", contents); } struct Config { query: String, filename: String, } fn parse_config(args: &[String]) -> Config { let query = args[1].clone(); let filename = args[2].clone(); Config { query, filename } }
clone性能不好,后面会介绍其他方式
There’s a tendency among many Rustaceans to avoid using clone
to fix ownership problems because of its runtime cost.
use std::env; use std::fs; fn main() { let args: Vec<String> = env::args().collect(); let config = Config::new(&args); println!("Searching for {}", config.query); println!("In file {}", config.filename); let contents = fs::read_to_string(config.filename) .expect("Something went wrong reading the file"); println!("With text: {}", contents); } struct Config { query: String, filename: String, } impl Config { fn new(args: &[String]) -> Config { let query = args[1].clone(); let filename = args[2].clone(); Config { query, filename } } }
添加自定义错误
use std::env; use std::fs; fn main() { let args: Vec<String> = env::args().collect(); let config = Config::new(&args); println!("Searching for {}", config.query); println!("In file {}", config.filename); let contents = fs::read_to_string(config.filename) .expect("Something went wrong reading the file"); println!("With text: {}", contents); } struct Config { query: String, filename: String, } impl Config { fn new(args: &[String]) -> Config { if args.len() < 3 { panic!("not enough arguments"); } let query = args[1].clone(); let filename = args[2].clone(); Config { query, filename } } }