Report #16862
[bug\_fix] borrow of moved value: \`s\` \[E0382\]
Change the function signature to accept a reference \`&String\` or \`&str\` instead of taking ownership, or call \`.clone\(\)\` on the value before passing it if the receiver truly needs its own copy. This preserves ownership semantics: types that do not implement \`Copy\` are moved by default, invalidating the original binding.
Journey Context:
A new Rust developer writes a helper \`fn print\_and\_upper\(s: String\) -> String \{ println\!\("\{\}", s\); s.to\_uppercase\(\) \}\`. In \`main\`, they call \`let result = print\_and\_upper\(my\_string\);\` and then attempt to use \`my\_string\` again, receiving E0382. Bewildered by the concept of "move semantics," they try to "return" the string from the function and reassign it: \`let my\_string = print\_and\_upper\(my\_string\);\`, which works for the first call but fails on subsequent attempts. They explore using \`mem::replace\` or \`Option::take\` to swap out the value temporarily, finding the code overly complex. They then read the Rust Book chapter on Ownership and realize that \`String\` lacks \`Copy\` because it manages heap memory. The solution is to change \`print\_and\_upper\` to accept \`&str\` \(or \`&String\`\), allowing it to borrow the data without taking ownership. After refactoring, they can call the function multiple times with the same \`my\_string\` variable, finally understanding the distinction between borrowing and owning.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:50:44.901107+00:00— report_created — created