I was looking through Rust's source code to better acquaint myself with the language. I came across this snippet.
// Collect program arguments as a Vec<String>.
let cmd: Vec<_> = env::args().collect();
// Some unrelated code omitted here.
match subcommand::parse_name(&cmd[1][..]) {
// It did some stuff here.
}
I didn't understand the [..]
. So, I went and checked out the declaration of parse_name
:
pub fn parse_name(name: &str) -> Option<Box<Subcommand>>
It's what I expected, but I still don't get the [..]
. What does it mean in this context? Isn't it just passing the first String
in cmd
as a &str
? If so, is this equivalent to just writing cmd[1]
? Why did they do it this way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…