I am trying to implement a dynamic programming problem in Rust to get to know the language. Like many problems with dynamic programming, memoization is used to reduce runtime. Unfortunately, my solution to the first exit gives errors. I fixed the code until the next. Warning - now this is a little pointless:
use std::collections::HashMap; fn repro<'m>(memo: &'m mut HashMap<i32, Vec<i32>>) -> Option<&'m Vec<i32>> { { let script_a = repro(memo); let script_b = repro(memo); } memo.get(&0) } fn main() {}
Compilation Error:
error[E0499]: cannot borrow `*memo` as mutable more than once at a time --> src/main.rs:6:30 | 5 | let script_a = repro(memo); | ---- first mutable borrow occurs here 6 | let script_b = repro(memo); | ^^^^ second mutable borrow occurs here 7 | } | - first borrow ends here
Why is the memo variable borrowed several times? In my opinion, it should be borrowed once when I calculate script_a , then this loan ends, then it is borrowed again for script_b .
rust
Shepmaster
source share