use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
if let Ok(file) = File::open("file.bdf") {
let reader = BufReader::new(file);
let satirlar: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
let mut GRIDS: Vec<Vec<&str>> = Vec::new();
let mut CQUAD4: Vec<Vec<&str>> = Vec::new();
let mut CBAR: Vec<Vec<&str>> = Vec::new();
let mut RBE3: Vec<Vec<&str>> = Vec::new();
for (index, satir) in satirlar.iter().enumerate() {
let a: Vec<&str> = satir.split_whitespace().collect();
let temp = format!("{} {}", satir, &satirlar[index + 1]);
let rbe3_concat: Vec<&str> = temp.split_whitespace().collect();
if a[0] == "GRID" {
GRIDS.push(a.clone());
}
else if a[0] == "CQUAD4" {
CQUAD4.push(a.clone());
}
else if a[0] == "CBAR" {
CBAR.push(a.clone());
}
else if a[0] == "RBE3" {
RBE3.push(rbe3_concat.clone());
}
}
error[E0597]: `temp` does not live long enough
--> src\main.rs:17:42
|
16 | let temp = format!("{} {}", satir, &satirlar[index + 1]);
| ---- binding `temp` declared here
17 | let rbe3_concat: Vec<&str> = temp.split_whitespace().collect();
| ^^^^ borrowed value does not live long enough
...
28 | RBE3.push(rbe3_concat.clone());
| ---- borrow later used here
29 | }
30 | }
| - `temp` dropped here while still borrowed
i couldn't solve this problem. Is there anyone who can help? If it's RBE3, it should be added with the next line... I haven't added the other parts of the code; I'm experiencing an issue here.
You are trying to save a reference to memory (
&str) outside of the block it is declared in, in this case the scope of theforloop. That simply isn't going to work. You need the Vecs outside the loop to own the memory or a reference-counted pointer to some heap allocated memory:Playground
Note the change in the Vec type and note that we map
.to_ownedover the sub chunks. Also note that we need to take care not to iterate past the end of the array. Also note that your ALL CAPS names get rejected by rustfmt... which is why I changed it in the code in my answer.You could as I said before alternatively allocate the data on the heap and store a reference-counted pointer to it.