-1

Compiler says there's temporary value that ends lifetime but i don't know how to prevent this.

fn connectPesel(Names: &mut Vec<&str>){
    let mut rng = rand::thread_rng();
    for i in 0..Names.len(){
        Names[i] = format!("{} {}", Names[i], rng.gen_range(1000..9999)).as_str();
    }
}

13 | fn connectPesel(Names: &mut Vec<&str>){
   |                                     - let's call the lifetime of this reference `'1`
...
16 |         Names[i] = format!("{} {}", Names[i], rng.gen_range(1000..9999)).as_str();
   |         -----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------- temporary value is freed at the end of this statement
   |         |          |
   |         |          creates a temporary which is freed while still in use
   |         assignment requires that borrow lasts for `'1`
   |

urioTV
  • 1
  • 1

1 Answers1

0

Try changing the type of Names to &mut Vec<String>, since the caller needs to be responsible for freeing the memory once it is done using the string, rather than freeing it immediately. (See https://dev.to/stevepryde/rust-string-vs-str-1l93)

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45