The value you create here gets dropped at the end of the current iteration of the loop. Therefore you can't keep references to it for longer than that, they'd be dangling. Just store owned Strings in your InterVal instead:
const N: u8 = 3;
pub struct InterVal {
// ...
pub name: [String; N as usize],
// ...
}
fn main() {
println!("N = {:?}\n", N);
let mut val_i: InterVal = InterVal {
name: core::array::from_fn(|_| "".to_owned()),
};
println!("val_i.name = {:?}", &val_i.name);
let mut i: usize = 0;
// let t01 = "name_".to_string() + &(i+1).to_string();
// let t02 = t01.as_str();
loop {
if i == N as usize {
break;
}
// Block_1:
//val_i.name[i] = "name_";
// Block_2:
val_i.name[i] = format!("name_{}", i + 1);
i = i + 1;
}
println!("val_i.name = {:?}", &val_i.name);
}
In your example you are creating a heap allocated string. You just stored a reference to the heap allocation, instead of taking the String instance by ownership. You could use something like arrayvec::ArrayString to create stack-allocated strings instead. I'd try to measure the performance of String vs ArrayString for real-life workloads of your program, just to be sure whether stack-allocated strings are a worthwhile optimization.