I am reading the book and in chapter 13 I was wondering what would happen if I could omit the
reference & of the function argument - I have written the question into the code excerpt.
use std::env;
use std::fs;
fn main() {
let args: Vec<String> = env::args().collect();
let config = parse_config(&args);
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
let contents = fs::read_to_string(config.file_path)
.expect("Should have been able to read the file");
// --snip--
}
struct Config {
query: String,
file_path: String,
}
// if we omit the & in &[String] how is ownership passed around? from the input to query/file_path to //Config? Would there be the need for a lifetime reference 'a for Config? But the compiler would not know
//the end of the lifetime reference right?
fn parse_config(args: &[String]) -> Config {
let query = args[1].clone();
let file_path = args[2].clone();
Config { query, file_path }
}
If you could pass a [String], the slice would own the Strings. But you can't moved unsized things like [String]s. The closest things I can think of which you can actually do are arrays or Box<[String]>.
Generally it's a good idea to use references for function argument types.
The exceptions are:
if the function is going to keep the value (e.g. it's a constructor or a setter). In such case it would need to clone the argument, because in most cases it can't keep a temporary reference. In that case you can make the argument an owning type, or a generic like impl Into<Vec<String>>.
if the type is an integer or a very small array or tuple of small values, or a special SIMD type. When argument types fit in registers that's usually more efficient than passing them by reference.