The I/O project example in the Rust book suggests cloning the command line args:
fn main() {
let args: Vec<String> = env::args().collect();
let config = parse_config(&args);
// ...
}
struct Config {
query: String,
filename: String,
}
fn parse_config(args: &[String]) -> Config {
let query = args[1].clone();
let filename = args[2].clone();
Config {
query, filename
}
}
I would like to move the values into the Config
struct to avoid the copy.
I tried moving the slice:
fn parse_config(args: [String]) -> Config
But got the error:
the trait `std::marker::Sized` is not implemented for `[std::string::String]`
Which makes sense because it's now reading as an unsized array. Next I tried generics:
fn parse_config<T: std::ops::Index<usize, Output=String> + Sized>(args: T) -> Config
Which gives the error:
cannot move out of indexed content
This also makes sense, as it would leave the vector in an invalid state. I could move the vector:
fn parse_config(mut args: Vec<String>) -> Config {
let query = args.remove(1);
// ...
But now the function is tied to the particular container Vector
.
How would I write a function that consumes the vector and allows me to move out its contents, while still preserving the generic nature of the function?