Error E0505

"cannot move out of because it is borrowed" — How to Fix

Fix Rust error E0505 by cloning the value or restructuring code to avoid moving a borrowed variable.

Fix E0505 by ensuring the value is not moved while a reference to it is still active. You must either clone the value before moving it or restructure the code so the reference is no longer needed when the move occurs.

fn main() {
    let s = String::from("hello");
    let r = &s; // Borrow s
    let s2 = s.clone(); // Clone s instead of moving it
    println!("{r}, {s2}");
}