러스트 공부 3주차인데 템플릿 캐시 구조체 만들다가 막혔습니다. 맵에 있으면 꺼내주고 없으면 로드해서 넣은 다음 돌려주는 함수에요
struct TemplateCache {
map: HashMap<String, Template>,
}
impl TemplateCache {
fn get_or_load(&mut self, name: &str) -> &mut Template {
if let Some(t) = self.map.get_mut(name) {
return t;
}
self.map.insert(name.to_string(), Template::load(name));
self.map.get_mut(name).unwrap()
}
}error[E0499]: cannot borrow `self.map` as mutable more than once at a time
--> src/cache.rs:22:9
|
18 | fn get_or_load(&mut self, name: &str) -> &mut Template {
| - let's call the lifetime of this reference `'1`
19 | if let Some(t) = self.map.get_mut(name) {
| -------- first mutable borrow occurs here
20 | return t;
| - returning this value requires that `self.map` is borrowed for `'1`
21 | }
22 | self.map.insert(name.to_string(), Template::load(name));
| ^^^^^^^^ second mutable borrow occurs here
error[E0499]: cannot borrow `self.map` as mutable more than once at a time
--> src/cache.rs:23:9
|
...
For more information about this error, try `rustc --explain E0499`.
error: could not compile `tplsrv` (bin "tplsrv") due to 2 previous errorsif let 안에서 return 했으니까 아래로 내려왔으면 첫번째 빌림은 끝난거 아닌가요? 논리적으로는 문제 없어 보이는데 왜 안되는지 모르겠습니다.. gpt한테 물어보니 clone 하라고 하는데 Template이 꽤 커서 clone은 왠만하면 안하고 싶어요 ㅠㅠ
댓글 0