Hi, I have some bioinformation algorithm that I am trying to implement. I take a long string and find to count occurances of lenght k. If they appear more than t times I put them in a Set.
I can't figure out the right borrowing thing to do:
fn clump_finding(text: String, k: usize, t: i32)
{
//let mut res = Vec::new();
let tlen = text.len();
let mut my_collect = HashMap::new();
let mut my_set = HashSet::new();
// First round go over the first L character
for j in 0..(tlen-k+1)
{
let pattern = &text[j as usize..(j + k) as usize];
if !my_collect.contains_key(&pattern)
{
my_collect.insert(&pattern, 1);
}
else
{
let e1 = my_collect.get(&pattern).unwrap();
*e1 += 1;
if *e1 >= t { my_set.insert(&pattern); }
}
}
}
The errors I get:
error[E0594]: cannot assign to `*e1` which is behind a `&` reference
--> src\ba_problems.rs:315:13
|
314 | let e1 = my_collect.get(&pattern).unwrap();
| -- help: consider changing this to be a mutable reference: `&mut i32`
315 | *e1 += 1;
| ^^^^^^^^ `e1` is a `&` reference, so the data it refers to cannot be written
error[E0597]: `pattern` does not live long enough
--> src\ba_problems.rs:316:41
|
316 | if *e1 >= t { my_set.insert(&pattern); }
| ------ ^^^^^^^^ borrowed value does not live long enough
| |
| borrow later used here
317 | }
318 | }
| - `pattern` dropped here while still borrowed