Help in fix - cannot borrow data in a `&` reference as mutable?

⚓ Rust    📅 2025-10-01    👤 surdeus    👁️ 9      

surdeus

I already had mutable data in enum, but forgot how to handle them. The enum is defined as:

#[derive(PartialEq, Debug)]
pub enum OptVal {
    Num(i64),
    FNum(f64),
    Str(String),
    Arr(HashSet<(String,String)>),
    Empty,
    Unmatch
}

I use placing the set as:

if opt.v.is_none() {
       opt.v = Some(OptVal::Arr(HashSet::new()))
} 

However when I try to modify the set:

match &opt.v {
        &Some(OptVal::Arr(ref mut set)) => {

I get an error:

error[E0596]: cannot borrow data in a `&` reference as mutable
   --> src/cli.rs:144:47
    |
144 | ...                   &Some(OptVal::Arr(ref mut set)) => {
    |                                         ^^^^^^^^^^^ cannot borrow as mutable

How can I correct it?

1 post - 1 participant

Read full topic

🏷️ Rust_feed