What does the double ellipsis operator mean in the context of the & str index?
I searched the Rust source code to get a better understanding of 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 get it [..]
, so I went and checked the parse_name () declaration:
pub fn parse_name(name: &str) -> Option<Box<Subcommand>>
This is what I expected, but I still don't get it [..]
. What does this mean in this context? Isn't that just passing the first line to cmd
how &str
? If so, is it equivalent to writing cmd[1]
? Why would they do that?
source to share
It is just a way of being forced from String
to &str
. In this case, [..]
it is not actually required, since Deref
coercion means that it is parse_name(&args[1])
also valid: &String
implicitly borrows &str
.
The indexing operator [ ]
calls std::ops::Index
, and the syntax ..
creates a std::ops::RangeFull
value . cmd
is Vec<String>
because it std::env::args()
returns a Iterator
through String
s .
Hence the syntax foo[..]
calls the implementation Index<RangeFull>
for String
(which you can see in the list of executors on the page Index
). The implementation looks like this:
impl ops::Index<ops::RangeFull> for String {
type Output = str;
#[inline]
fn index(&self, _index: ops::RangeFull) -> &str {
unsafe { mem::transmute(&*self.vec) }
}
}
&*self.vec
borrows the String
inner one Vec<u8>
before &[u8]
and then transmute
explicitly passes it to &str
, which is safe because the String
API makes sure the inner one Vec<u8>
is UTF-8, which is what is required str
.
source to share
Two periods ( ..
) is a range operator. There are four options:
If no element is in the ending position, the range continues "forever".
This is combined with Index
trait (or IndexMut
mutation if required). In your example you have a cut line (type, see next point.), To which you apply indexing: "foo"[2..]
.
In particular, it &str
implements Index
as
Returns a slice of the given string from a range of bytes
Then comes the third bit of ergonomics: Deref
(or DerefMut
similar cases). String
implements Deref
, by returning &str
, so whatever method is available for &str
is available for String
.
source to share