Trying to get a mutable String (or maybe &mut str) from an enum
⚓ Rust 📅 2025-12-24 👤 surdeus 👁️ 3I have this enum
pub enum ArgVal<'argval> {
...
RustString(&'argval String),
None,
...
}
I have a Vec of them in a struct
pub struct FuncDef<'argval> {
...
pub(crate) arg_vals: Vec<ArgVal<'argval>>,
...
}
I need to perform an update on the string if I have an ArgVal::RustString. I cannot find the correct incantation. Here is my current effort
// I know val_idx is valid
let arg_str = self.arg_vals.get_mut(val_idx).unwrap();
if let ArgVal::RustString(str) = arg_str {
unsafe {
str.as_mut_vec().set_len(len);
}
}
I get
error[E0596]: cannot borrow `**str` as mutable, as it is behind a `&` reference
--> src\caller.rs:121:33
|
121 | ... str.as_mut_vec().set_len(len);
| ^^^ cannot borrow as mutable
Hovering over str shows that it is an &mut &String, which isnt right. I need a &mut String.
I have tried changing RustString to be &'argval str but it makes no difference.
Note that I want to update my callers String (like read_line does) , hence the reference in the enum, not a String instance.
5 posts - 3 participants
🏷️ Rust_feed