I have the following code:
pub type Blockchain<T> = Vec<Block<T>>;
pub fn blockchain() -> Blockchain<String> {
let size = 10;
let mut chain: Blockchain<String> = Vec::with_capacity(size);
chain.push(Block::genesis());
for i in 0..(size-1) {
match chain.last_mut() {
Some(tip) => chain.push(tip.next_block(String::from("yooooo"))),
None => {}
}
}
chain
}
I'm getting an error:
error[E0499]: cannot borrow `chain` as mutable more than once at a time
--> src/blockchain/mod.rs:33:26
|
32 | match chain.last_mut() {
| ----- first mutable borrow occurs here
33 | Some(tip) => chain.push(tip.next_block(String::from("yooooo"))),
| ^^^^^ second mutable borrow occurs here
34 | None => {}
35 | }
| - first borrow ends here
How can I validly implement this in Rust?? So far I've tried using Box
, Rc
, and RefCell
, with no luck.