Hi all. I should start by saying that I am new to Rust, so this question might be too obvious! ![]()
Regardless, all I am doing is a function that receives a vector by reference and I just use find to find an element and then return it:
fn get_file_from_list(list: &Vec<ZippedFile>, filename: &str) -> ZippedFile {
let result = list.iter().find(|&f| f.path == filename).unwrap();
return result;
}
But the compiler then complains:
error[E0308]: mismatched types
--> src/main.rs:78:12
|
76 | fn get_file_from_list(list: &Vec<ZippedFile>, filename: &str) -> ZippedFile {
| ---------- expected `ZippedFile` because of return type
77 | let result = list.iter().find(|&f| f.path == filename).unwrap();
78 | return result;
| ^^^^^^ expected struct `ZippedFile`, found `&ZippedFile`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
It says that the return type should be &ZippedFile and I don't understand why, and it doesn't seem to be the most correct thing to actually return &ZippedFile, I mean... the compiler doesn't even let you do that, while recommending doing this instead:
fn get_file_from_list<'a>(list: &'a Vec<ZippedFile>, filename: &'a str) -> &'a ZippedFile {
let result = list.iter().find(|&f| f.path == filename).unwrap();
return result;
}
But I must be honest and say that I don't undestand and don't know if the suggestion is the ideal thing or if there is a simpler way of doing this.
Thanks in advance!