我对Rust 还不熟悉,我试图理解什么时候应该使用"展开"和"预期".
下面是一个示例代码:
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let query = args.get(1).unwrap();
println!("query from unwrawp: {}", query);
let query = args.get(1).expect("insufficient arguments");
println!("query from expect: {}", query);
//$ cargo run hello
//OUTPUT:
//query from expect: hello
//query from unwrawp: hello
}
Only difference I observed is there's a custom panic message in expect.
Are these two interchangeable or are there any special scenarios where we should use one over the other?