-2

Getting the below error when trying to push more elements to a vector. How to overcome this error

error[E0506]: cannot assign to `x` because it is borrowed

x = format!(r"\*START TIME*{:?}\S*\s+(?P<Value>[a-zA-Z0-9_\-\.]+)", ts);
   |         ^^ assignment to borrowed `x` occurs here
76 |         core_regex_dict.push(&x);
   |         ---------------      --- borrow of `x` occurs here
   |         |
   |         borrow later used here

Code:

let mut x = String::new();  
    for ts in test_list {
        x = format!(r"\*START TIME*{:?}\S*\s+(?P<Value>[a-zA-Z0-9_\-\.]+)", ts);
        core_regex_dict.push(&x);  
    }
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I know very little of Rust (I don't know why SO put that question in my home page), but aren't you supposed to use `&mut x` instead of `$x` for mutable references ? – Seblor Nov 05 '19 at 17:52
  • x has been declared as a mut so adding &mut x might show an error of mutable variable twice. (Not sure why OS placed the question in your home page thou) – Monther Enayah Nov 05 '19 at 17:55
  • 1
    Welcome to Stack Overflow! It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Nov 05 '19 at 17:56
  • Welcome to Stack Overflow! It looks like your question might be answered by the answers of [How does assigning to a borrowed variable violate the rules of references?](https://stackoverflow.com/q/46157422/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 05 '19 at 17:58
  • See also [Cannot assign to `self.x` because it is borrowed](https://stackoverflow.com/q/47133648/155423); [Cannot assign to variable because it is borrowed](https://stackoverflow.com/q/40309585/155423); etc. – Shepmaster Nov 05 '19 at 17:59

2 Answers2

-1

It's hard to answer your question because you didn't give us enough information. In particular, we need to know how core_regex_dict is defined. I'm assuming that core_regex_dict has type Vec<&str>. You need to change that to Vec<String>:

let core_regex_dict = Vec::new(); // No need to specify the item type, it will be inferred.
// No need to declare `x` before the loop unless you need to use the 
// last value afterward...
for ts in test_list {
    let x = format!(r"\*START TIME*{:?}\S*\s+(?P<Value>[a-zA-Z0-9_\-\.]+)", ts);
    core_regex_dict.push (x);  // No `&` -> we push the String itself instead of a reference
}
Jmb
  • 18,893
  • 2
  • 28
  • 55
-4

Found a solution that involves leaking the memory of the String in https://stackoverflow.com/a/30527289/12323498

But, is there a better way to achieve this without leaking the memory?

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}

and the code look like this now

let mut s = String::new();
    for ts in test_list {
        s = format!(r"\*START TIME*{:?}\S*\s+(?P<Value>[a-zA-Z0-9_\-\.]+)", ts);
        let s: &'static str = string_to_static_str(s);
        core_regex_dict.push(s);  
    }