This triggers the compiler to suggest cloning data
before passing it to a function:
// v1: from get() in hashmap
let data_type = match args.entries.get("data") {
Some(value) => value,
None => panic!("Error (none): args.entries.data."),
};
let data = match data_type {
ArgsType::Data(value) => value,
};
This does not:
// v2: from get_data() in struct implementation
let data = match args.get_data() {
Some(value) => value,
None => panic!("Error (none): args.data."),
};
Error:
--> src/model/mod.rs:35:45
|
35 | let data = my_func(data);
| ----------------- ^^^^^^^^^ expected `Data`, found `&Data`
| |
| arguments to this method are incorrect
|
note: method defined here
--> src/model/types/model/mod.rs:71:12
|
71 | pub fn my_func(&mut self, mut data: Data) -> Data {
| ^^^^^^^^^^^^^^^^^ -----------------------
help: consider using clone here
|
35 | let data = my_func(data.clone());
- Why?
- Can I avoid the extra clone somehow?
- How did I end up with
&Data
instead ofData
when I didn't specify&
in line 35?