Reputation: 7402
I am very new to rust and trying to write a command line utility as a way to learn.
I am getting the list of args
and trying to match on them
let args = os::args()
//some more code
match args[1].into_ascii_lower().as_slice() {
"?" | "help" => { //show help },
"add" => { //do other stuff },
_ => { //do default stuff }
}
this causes this error
cannot move out of dereference (dereference is implicit, due to indexing)
match args[1].into_ascii_lower().as_slice() {
^~~~~~~
I have no idea what that means, but searching yield this which I didn't completely get, but changing the args[1]
to args.get(1)
gives me another error
error: cannot move out of dereference of `&`-pointer
match args.get(1).into_ascii_lower().as_slice() {
^~~~~~~~~~~
what's going on?
Upvotes: 3
Views: 511
Reputation: 15002
As you can see in the documentation, the type of into_ascii_lower()
is (see here) :
fn into_ascii_upper(self) -> Self;
It takes self
directly, not as a reference. Meaning it actually consumes the String and return an other one.
So, when you do args[1].into_ascii_lower()
, you try to directly consume one of the elements of args
, which is forbidden. You probably want to make a copy of this string, and call into_ascii_lower()
on this copy, like this :
match args[1].clone().into_ascii_lower().as_slice() {
/* ... */
}
Upvotes: 3