我正在使用Rust编程,尝试使用以下代码访问第一个命令行参数:
use std::env;
fn main() {
let args: Vec<_> = env::args().collect();
let dir = args[1];
}
我遇到了这个错误:
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`
或者在 Rust 的后续版本中:
error[E0507]: cannot move out of index of `std::vec::Vec<std::string::String>`
--> src/main.rs:5:15
|
5 | let dir = args[1];
| ^^^^^^^
| |
| move occurs because value has type `std::string::String`, which does not implement the `Copy` trait
| help: consider borrowing here: `&args[1]`
如果我把它改为 let ref dir
,它就可以编译了,但我不明白正在发生什么。有人能解释一下"indexed content"是什么意思吗?
Vec
,你可以使用remove
方法。否则,你可以使用mem::replace
将值替换为虚拟值。 - oli_obkvec.into_iter().nth(1).expect("Missing element")
的作用是什么? - Tomáš Dvořák