#[cfg(test)]
mod tests {
use std::fs::File;
use std::path::Path;
use wax::{Glob, Pattern};
use zip::read::ZipFile;
use zip::ZipArchive;
#[test]
fn it_works() {
let dex = Glob::new("*.dex").unwrap();
let path = Path::new("/Users/leojangh/Downloads/sample.apk");
let mut archive = ZipArchive::new(File::open(path).unwrap()).unwrap();
let dex_files: Vec<_> = archive.file_names()
.filter(|&name| dex.is_match(name))
.map(|x| archive.by_name(x).unwrap())
.collect();
}
}
This code cannot be compiled:
error[E0502]: cannot borrow `archive` as mutable because it is also borrowed as immutable
--> src/lib.rs:21:18
|
19 | let dex_files: Vec<_> = archive.file_names()
| -------------------- immutable borrow occurs here
20 | .filter(|&name| dex.is_match(name))
21 | .map(|x| archive.by_name(x).unwrap())
| --- ^^^ ------- second borrow occurs due to use of `archive` in closure
| | |
| | mutable borrow occurs here
| immutable borrow later used by call
and
error: captured variable cannot escape `FnMut` closure body
--> src/lib.rs:21:22
|
18 | let mut archive = ZipArchive::new(File::open(path).unwrap()).unwrap();
| ----------- variable defined here
...
21 | .map(|x| archive.by_name(x).unwrap())
| - -------^^^^^^^^^^^^^^^^^^^^
| | |
| | returns a reference to a captured variable which escapes the closure body
| | variable captured here
| inferred to be a `FnMut` closure
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape
= note: requirement occurs because of the type `ZipFile<'_>`, which makes the generic argument `'_` invariant
= note: the struct `ZipFile<'a>` is invariant over the parameter `'a`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
How could I reach my goal? It looks like the function passed into map will be invoked many times,but the method by_name
has a &mut self
reference? I can't realize the second error message.