Avatar ·

Error "cannot move out of borrowed content" in Rust: causes and fix

Hi! I started learning Rust and encountered an error I can't understand. Code: ```rust fn main() { let s = String::from("hello"); let r = &s; let s2 = *r; // Error: cannot move out of borrowed content println!("{}", s2); } ``` I thought that `*r` dereferences the reference and gives me ownership of the string `s`. But the compiler says: "cannot move out of `*r` which is behind a shared reference". Why is that? I just want to copy the value. The same happens with vectors: ```rust fn get_first(v: &Vec) -> i32 { v[0] // Error: cannot move out of borrowed content } ``` I understand that Rust strictly tracks ownership, but I don't understand why you can't just "take" the value through a reference. In other languages (C++, Python) this works. How to properly