Existe uma maneira de liberar uma ligação antes que ela saia do escopo?
Estou tentando analisar um arquivo usando regexes:
extern crate regex; // 1.0.1
use regex::Regex;
fn example(
section_header_pattern: Regex,
section_name: &str,
mut line: String,
mut is_in_right_section: bool,
) {
loop {
if let Some(m) = section_header_pattern
.captures(&line)
.and_then(|c| c.get(1))
{
is_in_right_section = m.as_str().eq(section_name);
line.clear();
continue;
}
}
}
fn main() {}
... mas o compilador reclama porque oRegEx
écaptures()
O método tem um empréstimo que perdura por toda a vida útil da partida:
error[E0502]: cannot borrow `line` as mutable because it is also borrowed as immutable
--> src/main.rs:17:13
|
13 | .captures(&line)
| ---- immutable borrow occurs here
...
17 | line.clear();
| ^^^^ mutable borrow occurs here
18 | continue;
19 | }
| - immutable borrow ends here
Quando chego aline.clear();
, Eu terminei com oMatch
e gostaria de limpar o buffer e passar para a próxima linha do arquivo sem processamento adicional. Existe uma solução boa / limpa / elegante / idiomática ou preciso apenas morder a bala e introduzir um bloco subsequente "se"?