Error "cannot exit dereference" when trying to match strings from a vector
I am very new to rust and am trying to write a command line utility as a way of learning.
I am getting a list args
and trying to match 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 is causing 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 this means, but looking for the output of this , which I didn't get completely, but changing 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 happening?
source to share
As you can see in the documentation, the type into_ascii_lower()
( see here ):
fn into_ascii_upper(self) -> Self;
It accepts self
directly and not as a link. This means that it actually consumes a string and returns another.
So when you do args[1].into_ascii_lower()
, you are trying to directly use one of the elements args
, which is prohibited. You probably want to make a copy of this line and call into_ascii_lower()
on that copy, for example:
match args[1].clone().into_ascii_lower().as_slice() {
/* ... */
}
source to share