# error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable

  --> src/main.rs:10:5
   |
 8 |     let first = &vec[0];
   |                  ------ immutable borrow occurs here
 9 |     vec.push(1);
10 |     println!("{}", first);
   |     ^^^^^^^^^^^^^^^^^^^^^^^ immutable borrow later used here

- **ID:** `rust/e0502-borrow-mut-and-immut-in-vec`
- **Domain:** rust
- **Category:** type_error
- **Error Code:** `E0502`
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

Holding an immutable reference to a `Vec` element while trying to mutate the `Vec` (e.g., push), which could invalidate the reference.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| rustc 1.74 | active | — | — |
| rustc 1.75 | active | — | — |

## Workarounds

1. **Clone the element you need before mutating: `let first = vec[0].clone(); vec.push(1); println!("{}", first);`** (90% success)
   ```
   Clone the element you need before mutating: `let first = vec[0].clone(); vec.push(1); println!("{}", first);`
   ```
2. **Use indices instead of references: `let first_index = 0; vec.push(1); println!("{}", vec[first_index]);`** (85% success)
   ```
   Use indices instead of references: `let first_index = 0; vec.push(1); println!("{}", vec[first_index]);`
   ```

## Dead Ends

- **** — Leads to undefined behavior; the Vec may reallocate and invalidate the pointer. (90% fail)
- **** — Inefficient and doesn't solve the borrow conflict; still need to manage lifetimes. (50% fail)
