Goal Summary: I want to get rid of this error, and have my code to compile.
error[E0502]: cannot borrow `selected` as immutable because it is also borrowed as mutable
--> src/main.rs:17:26
|
16 | for tx in selected.iter_mut() {
| -------------------
| |
| mutable borrow occurs here
| mutable borrow later used here
17 | tx.1.edges.push((selected[0].0.clone(), selected[1].0.clone()));
| ^^^^^^^^ immutable borrow occurs here
error[E0502]: cannot borrow `selected` as immutable because it is also borrowed as mutable
--> src/main.rs:17:49
|
16 | for tx in selected.iter_mut() {
| -------------------
| |
| mutable borrow occurs here
| mutable borrow later used here
17 | tx.1.edges.push((selected[0].0.clone(), selected[1].0.clone()));
| ^^^^^^^^ immutable borrow occurs here
Minimum Reproducible Code:
use std::time::{Duration};
pub struct Transaction {
pub sender: String,
pub receiver: String,
pub amount: f64,
pub signature: String,
pub status: &'static str,
pub weight: u32,
pub timestamp: (String, Duration),
pub index: u32,
pub edges: Vec<(String, String)>,}
fn main () {
let mut selected: Vec<(String, Transaction)> = Vec::new();
// The following vec has 2 items.
for tx in selected.iter_mut() {
tx.1.edges.push((selected[0].0.clone(), selected[1].0.clone()));
tx.1.status = "confirmed"}}
What I've tried: How do I fix cannot borrow `` as mutable because it is also borrowed as immutable
Cannot borrow as mutable because it is also borrowed as immutable
cannot borrow as mutable, as it is behind a `&` reference
None of them really helped me, because either I'm not smart enough to understand or it just wasn't implementable for me. (I mean this in the case that even though we are getting the same errors, it's caused by different things).