I am playing with Rust, and I'm trying to access the first command line argument with this code:
use std::env;
fn main() {
let args: Vec<_> = env::args().collect();
let dir = args[1];
}
And I get this error:
error[E0507]: cannot move out of indexed content
--> src/main.rs:5:15
|
5 | let dir = args[1];
| --- ^^^^^^^ cannot move out of indexed content
| |
| hint: to prevent move, use `ref dir` or `ref mut dir`
If I change it to let ref dir
, it compiles, but I don't grok what's going on. Could someone explain what "indexed content" means?
When you use an index operator (
[]
) you get the actual object at index location. You do not get a reference, pointer or copy. Since you try to bind that object with alet
binding, Rust immediately tries to move (or copy, if theCopy
trait is implemented).In your example,
env::args()
is an iterator ofString
s which is then collected into aVec<String>
. This is an owned vector of owned strings, and owned strings are not automatically copyable.You can use a
let ref
binding, but the more idiomatic alternative is to take a reference to the indexed object (note the&
symbol):Implicitly moving out of a
Vec
is not allowed as it would leave it in an invalid state — one element is moved out, the others are not. If you have a mutableVec
, you can use a method likeVec::remove
to take a single value out:For your particular problem, you can also just use
Iterator::nth
: