Creates a temporary which is freed while still in use

So I just want to say that I am new to rust so sorry if this is a really stupid question

but here is my code but I have been looking around and some people said to add clone to the end that did not work I also set it to a variable then then the value to the variable, I have been trying for like 1 hour and I can not find out what the problem is

Your mistake is using the &str type in a place that requires ownership of the string. Change the vector to be of type Vec<String>. I also removed some unnecessary .to_string() calls.

use std::ops::Add;

fn main() {
    let mut vec = Vec::new();
-   vec.push("15");
-   vec.push("+");
-   vec.push("5");
+   vec.push("15".to_string());
+   vec.push("+".to_string());
+   vec.push("5".to_string());
    let mut keep_going = true;
    while keep_going {
        println!("{:?}", vec);
        let mut skip =  false;
        for y in 0..vec.len() {
            if skip == false {
                if vec[y] == "+" {
-                   vec[y - 1] = &vec[y - 1].to_string().parse::<i32>().unwrap().add(vec[y + 1].to_string().parse::<i32>().unwrap()).to_string();
+                   vec[y - 1] = vec[y - 1].parse::<i32>().unwrap().add(vec[y + 1].parse::<i32>().unwrap()).to_string();
                    skip = true;
        println!("{:?}", vec);
        if vec.len() == 1 {
            keep_going = false;

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.